关于Vue在二次封装UI组件时,处理ref的问题

一、需求

二次封装elemtnui组件的文本框,供我们项目使用

二、实现

子组件实现(MyInput.vue);

<template>
  <el-input ref="childRef" v-bind="$attrs"></el-input>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref } from 'vue';
  /* 
    defineOptions参考插件 https://vue-macros.sxzz.moe/macros/define-options.html
    或者参考官网对setup中设置name的解释 https://cn.vuejs.org/api/options-misc.html#name
  */
  defineOptions({
    name: 'myInput'
  });
  const childRef = ref();
  const options = reactive<{[K: string]: any}>({});
  onMounted(() => {
    const entries = Object.entries(childRef.value);
    for(let [key, value] of entries){
      if(!value || typeof value !== 'function'){
        continue;
      }
      options[key] = value;
    }
  });
  defineExpose(options);
</script>

父组件实现

<template>
	<myInput ref="myInputRef" v-model="val" />
</template>

<script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import myInput from '@com/myInput/index.vue';
  defineOptions({
    name: 'xxx'
  });
  const myInputRef = ref();
  const val = ref(0);
  onMounted(() => {
    console.log(myInputRef.value); // 这里的就是el-input组件给我们提供的所有方法
  });
</script>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的例子,演示了如何在Vue3中使用Element UI进行二次封装: ```vue <template> <el-card> <el-row> <el-col :span="12"> <el-input v-model="inputValue" placeholder="请输入内容"></el-input> </el-col> <el-col :span="12"> <el-button @click="handleClick">搜索</el-button> </el-col> </el-row> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" ></el-pagination> </el-card> </template> <script> import { ref } from 'vue'; import { ElInput, ElButton, ElTable, ElTableColumn, ElPagination, ElRow, ElCol, ElCard } from 'element-plus'; export default { components: { ElInput, ElButton, ElTable, ElTableColumn, ElPagination, ElRow, ElCol, ElCard }, setup() { const inputValue = ref(''); const tableData = ref([ { name: '张三', age: 20, address: '北京市海淀区' }, { name: '李四', age: 25, address: '上海市浦东新区' }, { name: '王五', age: 30, address: '广州市天河区' }, ]); const currentPage = ref(1); const pageSize = ref(10); const total = ref(3); const handleClick = () => { // 搜索逻辑 }; const handleSizeChange = (val) => { pageSize.value = val; }; const handleCurrentChange = (val) => { currentPage.value = val; }; return { inputValue, tableData, currentPage, pageSize, total, handleClick, handleSizeChange, handleCurrentChange, }; }, }; </script> ``` 在这个例子中,我们引入了Element UI中的一些组件,如`ElInput`、`ElButton`、`ElTable`等。我们使用`ref`函数来创建响应式数据,并将其绑定到组件的属性或事件中。我们还可以在`setup`函数中定义方法来处理组件的事件,如`handleClick`、`handleSizeChange`、`handleCurrentChange`等。 通过这种方式,我们可以更加灵活地使用Element UI,并根据自己的需求进行二次封装,提高代码的可维护性和重用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Agwenbi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值