将心比心,方得人心~

通过插槽分发内容

周洲 2020-06-25 18:58:10

和 HTML 元素一样,我们经常需要向一个组件传递内容,比如页面原本内容是这样的:

Something bad happened.

但是我们想要的效果是这样的:

Error!Something bad happened.

幸好,Vue 自定义的 <slot> 元素让这变得非常简单:

Vue.component('alert-box', {
  template: `
    <div class="demo-alert-box">
      <strong>Error!</strong>
      <slot></slot>
    </div>
  `
})

html部分:

<alert-box>
  Something bad happened.
</alert-box>


打赏

『微信打赏』

Tag标签插槽 

我是有底线的