Vue 二次封装组件的方法(转载)

介绍

有的时候我们全局引用 UI 框架(类似于 ElementUI)的时候,有些简单的组件我们想进行二次封装,并确保能传递所需的 props 和事件,这时候就需要用到 vue 实例上的两个属性:$props和$listeners,最后全局注册组件覆盖掉原始的就好了。

代码

<template>
  <el-inputs
    v-model="currentValue"
    v-bind="$props"
    v-on="$listeners"
  ></el-inputs>
</template>
<script>
import { ElInput } from 'element-ui'

export default {
  name: 'ElInput',
  components: {
    ElInputs: ElInput,
  },
  props: {
    ...ElInput.props,
    value: [String, Array, Object, Number],
  },
  computed: {
    currentValue: {
      get() {
        return this.value
      },
      set(newV) {
        this.$emit('input', newV)
      },
    },
  },
}
</script>

最后

十分感谢原博主的博客,原博客地址为:
https://blog.csdn.net/zh_rey/article/details/106735049

posted @ 2021-01-14 17:58  DAmarkday  阅读(2061)  评论(0)    收藏  举报