Difference between .$mount() and el [Vue JS]

前端 未结 4 2330
北荒
北荒 2020-12-07 14:40

What\'s the difference between this code:

new Vue({
    data () {
        return {
            text: \'Hello, World\'
        };
    }
}).$mount(\'#app\')
         


        
4条回答
  •  不知归路
    2020-12-07 14:59

    According to the Vue.js API docs on vm.$mount(), the two are functionally the same, except that $mount can (optionally) be called without an element selector, which causes the Vue model to be rendered separate from the document (so it can be appended later). This example is from the docs:

    var MyComponent = Vue.extend({
      template: '
    Hello!
    ' }) // create and mount to #app (will replace #app) new MyComponent().$mount('#app') // the above is the same as: new MyComponent({ el: '#app' }) // or, render off-document and append afterwards: var component = new MyComponent().$mount() document.getElementById('app').appendChild(component.$el)

提交回复
热议问题