vue函数式组件用法

vue文档链接 functional

函数式组件最大的特点就是没有this,但是可以通过context获取组件的属性来进行操作。

如以下是一个空白的组件,直接返回组件内插槽的内容

Vue.component('NullComponent', {
  functional: true,
  // Props 是可选的
  props: {
    // ...
  },
  // 提供第二个参数作为上下文
  render: function (createElement, context) {
    const { props, scopedSlots } = context
    return ScopedSlots.default()
  }
})

使用场景

// 引入校验函数
import { check } from "../util/auth"
Vue.component('NullComponent', {
  functional: true,
  props: {
    authority: {
        type: Array,
        required: true
    }
  },
  // 提供第二个参数作为上下文
  render: function (createElement, context) {
    const { props, scopedSlots } = context
    return check(props.authority) ? ScopedSlots.default() : null
  }
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容