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

前端 未结 4 2327
北荒
北荒 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 15:01

    In the example you provide, I don't believe there is really much difference or benefit. However, in other situations there may be a benefit. (I have never encountered situations like the following).

    1. With $mount() you have more flexibility what element it will be mounted on if that were to ever be necessary.

    2. Similarly you if for some reason you need to instantiate the instance before you actually know what element it will be mounted on (maybe an element that is created dynamically) then you could mount it later using vm.$mount()

    3. Following along with the above you could use also use mount when you need to make a decision before hand which element to mount to assuming that there may be two or more possibilities.

    Something like...

    if(document.getElementById('some-element') != null){
          // perform mount here
    }
    

提交回复
热议问题