内联模板

inline-template 这个特殊的特性出现在一个子组件上是,这个组件将会使用其里面的内容做为模板,而不是将其作为被 分发的内容 。这使得模板的撰写工作更加灵活

<my-component inline-template>
  <div>
    <p>These are compiled as the component's own template.</p>
    <p>Not parent's transclusion content.</p>
  </div>
</my-component>

内联模板需要定义在 Vue 所属的 DOM 元素内

在阅读到这一节时,一个被分发的内容这个名词绕了一下。现结合场景进行了以下分析:

<my-component> 
 	内容代码...
 </my-component>

Vue 说明了如果 my-component 组件内未定义插槽,那么 “内容代码…” 将会被抛弃,如果定义了 <slot></slot> 普通插槽,那这里的内容的将会替换掉 <slot></slot> 所在的位置,那么,如果定义了多个具名插槽,“内容代码…” 将会根据代码含义分别替换具名插槽的位置。

也许是从这种机制中得出的灵感,分发 -> 分别发送给不同的插槽,因此将 组件的一对标签所包含的内容称为 分发的内容,或者更贴切的称为 待分发的内容

而这里所说的 这个组件将会使用其里面的内容作为模板,则是指使用 “内容代码…” 完全替换掉原组件的模板【template属性】的内容

<div id="app">
    <my-component inline-template>
      <div>
        <p>These are compiled as the component's own template.</p>
        <p>Not parent's transclusion content.</p>
      </div>
    </my-component>
  </div>
const vm = new Vue({
  el: '#app',
  components: {
    myComponent: { template: '<div>我是原先的内容</div>' },
  },
  data: {
  },
});

有趣的是使用 inline-template 属性后,分发的内容 将同 标签一样必须存在一个根元素。

以上属个人理解,如有错误,欢迎指正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值