和 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>