vue函数化组件 functional

须要在组件中把functional 设置为truejavascript

一个函数化组件像这样:java

Vue.component('testcomponent', {
 functional: true,
 // 为了弥补缺乏的实例
 // 提供第二个参数做为上下文
 render: function (createElement, context) {
  // ...
 },
 // Props 可选
 props: {
  level:{type:Number,default:1}
 }
})

组件须要的一切都是经过上下文传递,函数化组件只是一个函数,因此渲染开销也低不少数组

  • props: 提供props 的对象
  • children: VNode 子节点的数组
  • slots: slots 对象
  • data: 传递给组件的 data 对象
  • parent: 对父组件的引用

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段落标签。对象

相关文章
相关标签/搜索