Vue.prototype.appName = 'My App'
Then what would you expect to be logged below?
new Vue({
data: {
// Uh oh - appName is *also* the name of the
// instance property we just defined!
appName: 'The name of some other app'
},
beforeCreate() {
console.log(this.appName)
},
created() {
console.log(this.appName)
}
})
It would be "The name of some other app"
, then "My App"
, because this.appName
is overwritten ([sort of]
你确定不是先
My App
后The name of some other app
?beforeCreate
的时候没实例化,无appName
属性,会去原型链上找(即Vue上)