new Vue({ el: '#app', beforeCreate: function () { console.log('调用了beforeCreat钩子函数') }, created: function () { console.log('调用了created钩子函数') }, beforeMount: function () { console.log('调用了beforeMount钩子函数') }, mounted: function () { console.log('调用了mounted钩子函数') } })
new Vue({ beforeCreate: function () { console.log('调用了beforeCreat钩子函数') }, created: function () { console.log('调用了created钩子函数') }, beforeMount: function () { console.log('调用了beforeMount钩子函数') }, mounted: function () { console.log('调用了mounted钩子函数') } })
var vm = new Vue({ beforeCreate: function () { console.log('调用了beforeCreat钩子函数') }, created: function () { console.log('调用了created钩子函数') }, beforeMount: function () { console.log('调用了beforeMount钩子函数') }, mounted: function () { console.log('调用了mounted钩子函数') } }) vm.$mount('#app')
new Vue({ el: '#app', template: '<div id="app"><p>模板在templated参数中找到了哟~</p></div>' }) demo:
<div id="app"><p>模板是在外部HTML中找到的~</p></div> 建立对象实例: new Vue({ el: '#app' })
<div id="app"><p>模板是在外部HTML中找到的~</p></div>
new Vue({ el: '#app', template: '<div id="app"><p>模板在templated参数中找到了哟~</p></div>' })
new Vue({ el: '#demo', render (createElement) { return (....) } })
<div> <header> <h1>I'm a template!</h1> </header> <p v-if="message"> {{ message }} </p> <p v-else> No message. </p> </div>
function anonymous() { with(this){return _c('div',[_m(0),(message)?_c('p',[_v(_s(message))]):_c('p',[_v("No message.")])])} }
对于这一点, 我也感到有些迷惑,百度后以后也没什么头绪,最后我思考的结果是这样的:正由于render函数和template选项的“优先级”比外部HTML要高,因此,最后通常会存在一个外部HTML模板被Vue实例自己配置的模板所“替代”的过程也就是上图所说的 “replace”
var vm = new Vue({ el: '#app', data: { number: 1 }, template: '<div id="app"><p></p></div>', beforeUpdate: function () { console.log('调用了beforeUpdate钩子函数') }, updated: function () { console.log('调用了updated钩子函数') } }) vm.number = 2
var vm = new Vue({ el: '#app', data: { number: 1 }, // 在模板中使用number这个数据 template: '<div id="app"><p> {{ number }} </p></div>', beforeUpdate: function () { console.log('调用了beforeUpdate钩子函数') }, updated: function () { console.log('调用了updated钩子函数') } }) vm.number = 2