javascript - 在 Vue 中声明变量有什么区别?

标签 javascript vue.js vuejs2

您能否解释以不同方式声明变量之间的区别。我什么时候应该使用这些声明方式?

<script>
const someVariable = 12345

export default {
  name: 'App',
}
</script>
<script>

export default {
  name: 'App',
  data() {
    return {
      someVariable: 12345
    }
  }
}
</script>

最佳答案

在第一个中,您不能使用 someVariable在您的模板中

<script>
const someVariable = 12345

export default {
  name: 'App',
}
</script>
<template> <p> {{someVariable}} </p> </template> //doesn't work
在 Vue3 中可用:
要使其正常工作,您可以添加 setup脚本中的关键字,但您必须使用 ref(...) 包装变量值或 reactive(...)如果你想让它响应更改 More info
<script setup>
const someVariable = 12345

export default {
  name: 'App',
}
</script>
<template> <p> {{someVariable}} </p> </template> //works (you can see the data)

关于javascript - 在 Vue 中声明变量有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69301906/

相关文章:

javascript - 从 props 访问过滤后的对象值 (JS/React)

javascript - 将数据 json 字符串传递给数字 Vue.js

javascript - 访问 Vuejs 组件内的元素

vue.js - Vue 警告 $listeners 和 $attrs 是只读的

javascript - vue.js 中的 Angular ng-container 等价物

javascript - Vue.js 应用程序中生产模式下静态文件的路径错误

JavaScript - 在主体上加载下一个数组元素

javascript - stub 获取单元测试与 enzyme 和玩笑发生 react

javascript - Node 脚本中的可用模块

vue.js - 分离条件渲染的开始和结束标签