学习优先度:★★★★★
难易度:★★★★☆
了解如何使用 new Vue() 构造函数创建 Vue 实例是开始使用 Vue.js 的基础。
new Vue() 构造函数用于创建一个新的 Vue 实例,它是任何 Vue 应用程序的核心。该实例将 Vue 应用程序绑定到 DOM 的一部分。
【代码示例】
// Create a new Vue instance
var app = new Vue({
// The DOM element to mount the Vue instance
el: '#app',
// The data object for the Vue instance
data: {
message: 'Hello Vue!'
}
});
【执行结果】
xml<div id="app">
{{ message }}
</div>
When the above code runs, the text "Hello Vue!" will be displayed inside the div with the id app.
new Vue() 构造函数接收一个选项对象。el 属性指定了要将 Vue 实例绑定到的 DOM 元素。data 属性包含将在 Vue 实例中使用的数据。数据对象中的消息属性可在 HTML 中使用双大括号 {{ message }} 进行访问。
【补充】
Vue 实例是任何 Vue 应用程序的核心。它负责管理数据、方法和生命周期钩子。
el 属性可以是 CSS 选择器字符串,也可以是对 DOM 元素的直接引用。
Vue.js 使用反应式数据绑定系统,这意味着数据属性的任何更改都会自动更新 DOM。