vue 中 ui 组件二次封装后 ref 怎么穿透到子组件里

情景:element-ui 二次封装了 el-table 组件,使用封装组件时,想要调用 el-table 组件内置的一些方法。只在封装组件上定义 ref 是拿不到 el-table 内置方法的。解决方法如下。

1. vue2

封装组件

<template>

  <el-table ref="innerComponentRef" v-bind="$attrs"></el-table>

</template>

<script>

  export default {

    data() {

      return {};

    },

    mounted() {

      const entries = Object.entries(this.$refs.innerComponentRef);

      for (const [key, value] of entries) {

        this[key] = value;

      }

    },

  };

</script>

使用组件

<template>

  <myTable ref="myTabletRef" />

</template>

<script>

  import myTable from "./myTable.vue";



  export default {

    components: { myTable },

    data() {

      return {

        val: 0,

      };

    },

    mounted() {

      console.log(this.$refs.myTabletRef);

      // 这里就可以获取到 el-table 组件提供的所有方法

    },

  };

</script>

2. vue3

封装组件

<template>

  <el-table ref="innerComponentRef" v-bind="$attrs"></el-table>

</template>

<script setup>

  import { ref } from "vue";

  const innerComponentRef = ref();

</script>

使用组件

<template>

  <mytable ref="mytableRef" />

</template>

<script setup>

  import { onMounted, ref } from "vue";

  import mytable from "@com/mytable/index.vue";



  const mytableRef = ref();



  onMounted(() => {

    for (const key in mytableRef.value.innerComponentRef) {

      this[key] = mytableRef.value.innerComponentRef[key];

    }

  });

</script>

相关推荐

  1. vue ui 封装 ref 怎么穿透组件

    2024-07-20 04:54:05       16 阅读
  2. 如何封装一个Vue3组件库?

    2024-07-20 04:54:05       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 04:54:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 04:54:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 04:54:05       45 阅读
  4. Python语言-面向对象

    2024-07-20 04:54:05       55 阅读

热门阅读

  1. 求职学习day6

    2024-07-20 04:54:05       19 阅读
  2. AI Native应用中的模型微调

    2024-07-20 04:54:05       17 阅读
  3. c语言之 *指针与 **指针

    2024-07-20 04:54:05       16 阅读
  4. 【WPF开发】上位机开发-串口收发

    2024-07-20 04:54:05       18 阅读
  5. Eureka: 微服务架构中的服务发现与注册实践

    2024-07-20 04:54:05       18 阅读
  6. egret 白鹭的编译太慢了 自己写了一个

    2024-07-20 04:54:05       19 阅读
  7. git泄露

    2024-07-20 04:54:05       19 阅读
  8. 交叉编译aarch64的Qt5.12.2,附带Mysql插件编译

    2024-07-20 04:54:05       17 阅读