el 和 template 同時(shí)出現(xiàn)
- 當(dāng)一個(gè)實(shí)例同時(shí)擁有 el 和 template, Vue 在編譯的時(shí)候
- Vue 會(huì)把 template 的實(shí)例全部替換到 el 掛載的內(nèi)容上, 包括掛載的標(biāo)簽也會(huì)被替換掉
<div id="app"></div>
<script>
const app = new Vue({
el: "#app",
template: `
<div>
<h2>message</h2>
<button @click="btnClick">button</button>
<h2>{{name}}</h2>
</div>
`,
data: {
message: "hello world",
name: "zjh"
},
methods: {
btnClick() {
console.log("click");
}
}
});
</script>
- 最終
<div id="app"></div>
會(huì)被替換成 template 中的模板實(shí)例