函数式组件 - vue2

114 阅读1分钟

用途:封装只作展示的组件。

种类:

  1. template中声明functional属性 父组件传递的属性用props.作展示。
  2. 不写template,在script中声明functional:true,通过render函数书写html结构。

核心代码

//种类1
<template functional>
    <div>
        {{ props.title }}
    </div>
</template>

//种类2
<script>
export default {
    functional:true,
    render(h,ctx){
        return h( 
        'div', // 元素名称
        {class:''}, // props
        [ // 内容
            h(标签名称,props,内容),
            h(标签名称,props,内容),
            ...
        ]
    }
}
</script>