- Vue.js 2 Web Development Projects
- Guillaume Chau
- 295字
- 2021-07-02 22:34:23
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.
- Oracle從新手到高手
- Oracle Database In-Memory(架構(gòu)與實(shí)踐)
- Python測試開發(fā)入門與實(shí)踐
- 羅克韋爾ControlLogix系統(tǒng)應(yīng)用技術(shù)
- 數(shù)據(jù)結(jié)構(gòu)(C語言)
- C++ 從入門到項(xiàng)目實(shí)踐(超值版)
- 單片機(jī)應(yīng)用與調(diào)試項(xiàng)目教程(C語言版)
- Node Cookbook(Second Edition)
- Learning YARN
- Julia 1.0 Programming Complete Reference Guide
- 深度探索Go語言:對(duì)象模型與runtime的原理特性及應(yīng)用
- 軟件工程與UML案例解析(第三版)
- HTML5移動(dòng)Web開發(fā)
- Python深度學(xué)習(xí)與項(xiàng)目實(shí)戰(zhàn)
- Mastering Data Analysis with R