我刚刚安装了Vue,并一直遵循一些教程,使用Vue -cli webpack模板创建一个项目。当它创建组件时,我注意到它将我们的数据绑定到以下内容中:
export default {
name: 'app',
data: []
}
而在其他教程中,我看到数据被绑定到:
new Vue({
el: '#app',
data: []
)}
区别是什么,为什么两者之间的语法看起来不同?我遇到了麻烦,让“新的Vue”代码从我正在使用的Vue -cli生成的App.vue标签内工作。
申报时:
new Vue({
el: '#app',
data () {
return {}
}
)}
这通常是应用程序的其余部分所继承的根Vue实例。它挂在html文档中声明的根元素上,例如:
<html>
...
<body>
<div id="app"></div>
</body>
</html>
另一种语法是声明一个以后可以注册和重用的组件。例如,如果你创建一个单独的文件组件,比如:
// my-component.js
export default {
name: 'my-component',
data () {
return {}
}
}
你可以稍后导入它并像这样使用它:
// another-component.js
<template>
<my-component></my-component>
</template>
<script>
import myComponent from 'my-component'
export default {
components: {
myComponent
}
data () {
return {}
}
...
}
</script>
另外,一定要将数据属性声明为函数,否则它们就不会是响应式的。
申报时:
new Vue({
el: '#app',
data () {
return {}
}
)}
这通常是应用程序的其余部分所继承的根Vue实例。它挂在html文档中声明的根元素上,例如:
<html>
...
<body>
<div id="app"></div>
</body>
</html>
另一种语法是声明一个以后可以注册和重用的组件。例如,如果你创建一个单独的文件组件,比如:
// my-component.js
export default {
name: 'my-component',
data () {
return {}
}
}
你可以稍后导入它并像这样使用它:
// another-component.js
<template>
<my-component></my-component>
</template>
<script>
import myComponent from 'my-component'
export default {
components: {
myComponent
}
data () {
return {}
}
...
}
</script>
另外,一定要将数据属性声明为函数,否则它们就不会是响应式的。