解析 DOM 模板时的注意事项

下图是 Vue的官网中 对解析 DOM 模板时的注意事项:
在这里插入图片描述

如果我们从以下来源使用模板的话,这条限制是不存在的” 对于这句话困扰了我一段时间,觉得有必要做一个记录:

<body>
  <div id="app">
	<!--  is="" 的方式可以避开一些潜在的浏览器 **解析规则** ,正确渲染元素 -->
    <table>
      <tr is="todo-item-template"></tr>
    </table>

    <!-- 方式一:直接在table下使用子组件 -->
    <table>
      <todo-item-template></todo-item-template>
    </table><hr>

    <!-- 方式二:通过template字符串 -->
    <todo-three></todo-three><hr>

    <!-- 方式三:通过<script type="text/x-template" id="todo-four"></script> -->
    <todo-four></todo-four>
  </div>
 </body>
<script type="text/x-template" id="todo-four-template">
  <table><todo-item-template></todo-item-template></table>
</script>
Vue.component('todo-item-template', {
 template: '<tr>通过控制台查看我是否正确渲染</tr>',
});
Vue.component('todo-four', {
  template: '#todo-four-template',
});
const vm = new Vue({
  el: '#app',
  components: {
    'todo-three': {
      template: '<table><todo-item-template></todo-item-template></table>',
    },
  },
});

通过控制台就可以发现只有方式一出现了问题,tr元素跑到了table元素的同级:

  • 字符串(例如:template: '...'
  • 单文件组件(.vue)【该方法需要使用 vue-cli 脚手架,上例中未进行演示】
  • <script type="text/x-template">
    因此官网说 “如果我们从以下来源使用模板的话,这条限制是不存在的”。

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值