我应该在 vue 3 ref 变量中使用 let 还是 const ?

Iva*_*OFF 7 coding-style vue.js vuejs3 vue-composition-api


我正在创建一个新的 Vue 3 项目,我看到很多人在网上声明这样的引用。
const myVariable = ref(false)

为什么我们在 Vue 3 中突然使用const ?
我知道refs以某种方式包装它们以使它们可编辑,但我仍然不明白为什么不这样声明它们:我
let myVariable = ref(false)

知道这对于 Vue 3 开发人员来说可能听起来是一个愚蠢的问题,但我无法理解将值更改为常量背后的原因

与此同时,我在组合 API 中使用 const 声明,但我想承认背后的原因

doe*_*ter 5

它的偏好,但使用的论点const是当值没有改变时,例如:

\n
const name = \'John\';\n\n// Shouldn\'t work.\nname = \'Bill\';\n
Run Code Online (Sandbox Code Playgroud)\n

使用 时ref(),您不会替换变量,而是替换属性

\n
const name = ref(\'John\');\n\nname.current = \'Bill\';\n
Run Code Online (Sandbox Code Playgroud)\n
\n

解释如下eslint

\n
\n

如果变量从未重新分配,则使用const声明会更好。

\n

const声明告诉读者,\xe2\x80\x9c这个变量永远不会被重新分配,\xe2\x80\x9d减少认知负荷并提高可维护性。

\n
\n

文档(在撰写本文时):https ://eslint.org/docs/latest/rules/prefer-const

\n