官术网_书友最值得收藏!

Creating an app

For now, we don't have any Vue app running on our web page. The whole library is based on Vue instances, which are the mediators between your View and your data. So, we need to create a new Vue instance to start our app:

// New Vue instance
var app = new Vue({
// CSS selector of the root DOM element
el: '#root',
// Some data
data () {
return {
message: 'Hello Vue.js!',
}
},
})

The Vue constructor is called with the new keyword to create a new instance. It has one argument--the option object. It can have multiple attributes (called options), which we will discover progressively in the following chapters. For now, we are using only two of them.

With the el option, we tell Vue where to add (or "mount") the instance on our web page using a CSS selector. In the example, our instance will use the <p id="root"> DOM element as its root element. We could also use the $mount method of the Vue instance instead of the el option:

var app = new Vue({
data () {
return {
message: 'Hello Vue.js!',
}
},
})
// We add the instance to the page
app.$mount('#root')

Most of the special methods and attributes of a Vue instance start with a dollar character.

We will also initialize some data in the data option with a message property that contains a string. Now the Vue app is running, but it doesn't do much, yet.

You can add as many Vue apps as you like on a single web page. Just create a new Vue instance for each of them and mount them on different DOM elements. This comes in handy when you want to integrate Vue in an existing project.

主站蜘蛛池模板: 册亨县| 都昌县| 章丘市| 马龙县| 射洪县| 邵阳市| 布拖县| 越西县| 安庆市| 齐齐哈尔市| 胶南市| 高台县| 通江县| 阳东县| 五大连池市| 广水市| 岳阳市| 怀集县| 黑龙江省| 榆中县| 卫辉市| 托克托县| 淮阳县| 陇南市| 青田县| 晋宁县| 治多县| 扎鲁特旗| 浦江县| 枣庄市| 道孚县| 睢宁县| 丹棱县| 尼玛县| 德令哈市| 绥阳县| 砚山县| 万山特区| 陇西县| 汕头市| 松滋市|