Vue.js Getting Started

Every Vue application needs to be implemented by instantiating Vue.

The syntax format is as follows:

var vm = new Vue({
  // 选项
})

Next, let's look at what is needed in the Vue constructor through an example:

Example

<div id="vue_det"> <h1>site : {{site}}</h1> <h1>url : {{url}}</h1> <h1>{{details()}}</h1> </div> <script type="text/javascript">var vm = new Vue({ el: '#vue_det', data: { site: "Example Tutorial", url: "www.example.com", alexa: "10000" }, methods: { details: function() { return this.site + " - It's not just about technology, but also about dreams!"; } } })</script>

Try it Yourself »

Click the "Try it Yourself" button to view the online example.

You can see that there is an `el` parameter in the Vue constructor, which is the id in the DOM element. In the above example, the id is `vue_det`, in the div element:

<div id = "vue_det"></div>

This means that all our subsequent modifications are within the specified div above, and the outside of the div is not affected.

Next, let's see how to define data objects.

dataUsed to define properties. The instance has three properties: site, url, alexa.

methodsUsed to define functions, which can return function values through return.

{{ }}Used to output object properties and function return values.

<div id="vue_det">
    <h1>site : {{site}}</h1>
    <h1>url : {{url}}</h1>
    <h1>{{details()}}</h1>
</div>

When a Vue instance is created, it adds all the properties that can be found in its data object to Vue's reactive system. When the values of these properties change, the HTML view will change accordingly.

Example

<div id="vue_det"> <h1>site : {{site}}</h1> <h1>url : {{url}}</h1> <h1>Alexa : {{alexa}}</h1> </div> <script type="text/javascript">// ourDataObject var data = { site: "Example", url: "www.example.com", alexa: 10000} var vm = new Vue({ el: '#vue_det', data: data }) // theyReferencesameobject of! document.write(vm.site === data.site) // true document.write("<br>") // Setting properties will also affect the original data vm.site = "Example" document.write(data.site + "<br>") // Example // ... and vice versa data.alexa = 1234 document.write(vm.alexa) // 1234</script>

Try it Yourself »

In addition to data properties, Vue instances also provide some useful instance properties and methods. They all have the prefix `$` to distinguish them from user-defined properties. For example:

Example

<div id="vue_det"> <h1>site : {{site}}</h1> <h1>url : {{url}}</h1> <h1>Alexa : {{alexa}}</h1> </div> <script type="text/javascript">// Our data object var data = { site: "Example Tutorial", url: "www.example.com", alexa: 10000} var vm = new Vue({ el: '#vue_det', data: data }) document.write(vm.$data === data) // true document.write("<br>") document.write(vm.$el === document.getElementById('vue_det')) // true </script>

Try it Yourself »
Other Extensions