What is the difference between new Vue ({}) and var app=new Vue ({}) in creating vue instances?

    <div id="app">
        

{{msg}}

</div> <script> var app = new Vue({ el: "-sharpapp", data: { msg: "Hello Vue" } }) app.msg=123 console.log(app.msg) console.log(app.$data.msg === app.msg) </script>

when the above code is created by var, the two print messages execute normally, and when created by new Vue, the second print message reports an error. What is the difference between the two methods?


is just new Vue ({}) instead of var app=new Vue ({}) assigned to the variable app , app is not actually an instance of Vue.
it is not actually created until app.msg=123 and does not have $data attribute


you might as well print app <

.
if the id of the dom element does not have the same name as the js built-in object, or is not overridden, then the "variable" of this id points to the dom element. That is, if app is not reassigned, app points to the dom element. Because the second method reassigns app , app points to the Vue object.
in addition, has a similar effect


el: "- sharpapp" means mounting to an element where id is app. Var app is an instance of app vue that is created. Try changing the two names differently

Menu