el 和 template 的区别

el 和 template 同时出现

  • 当一个实例同时拥有 eltemplate, Vue 在编译的时候
    • Vue 会把 template 的实例全部替换到 el 挂载的内容上, 包括挂载的标签也会被替换掉
<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>会被替换成 template 中的模板实例
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容