须要在组件中把functional 设置为truejavascript
一个函数化组件像这样:java
Vue.component('testcomponent', { functional: true, // 为了弥补缺乏的实例 // 提供第二个参数做为上下文 render: function (createElement, context) { // ... }, // Props 可选 props: { level:{type:Number,default:1} } })
组件须要的一切都是经过上下文传递,函数化组件只是一个函数,因此渲染开销也低不少数组
this.$slots.default 更新为 context.children,以后this.level 更新为 context.props.level。 函数
2、slots()和children对比this
slots().default
和 children
相似code
不一样之处在于:component
<createElement> <template #head>aaaaa</template> <p>bbbb</p> <template #footer>ccccc</template> </createElement>
children 会给你三个段落标签,而 slots().default 只会传递第二个匿名段落标签,slots().head会传递第一个具名为head段落标签。对象