当前位置: 首页 > news >正文

解决vue3使用ref 获取不到子组件属性问题

需求:

父子组件使用<script setup>语法糖,父组件通过给子组件定义ref访问子组件内部属性或事件。

关键点:

子组件中,setup语法糖需要用defineExpose把要读取的属性和方法单独暴露出去,否则会访问失败;如果子组件使用setup()函数,则在父组件通过ref可以直接访问其属性,不需要用defineExpose暴露数据。

子组件:src/components/BaseInfoDialog.vue

<template><el-dialog v-model="dialogTableVisible" title="Shipping address" width="800"><el-table :data="gridData"><el-table-column property="date" label="Date" width="150" /><el-table-column property="name" label="Name" width="200" /><el-table-column property="address" label="Address" /></el-table></el-dialog>
</template><script lang="ts" setup>
import { ref, defineExpose } from "vue";const dialogTableVisible = ref(false);const gridData = [{date: "2016-05-02",name: "John Smith",address: "No.1518,  Jinshajiang Road, Putuo District"},{date: "2016-05-04",name: "John Smith",address: "No.1518,  Jinshajiang Road, Putuo District"},{date: "2016-05-01",name: "John Smith",address: "No.1518,  Jinshajiang Road, Putuo District"},{date: "2016-05-03",name: "John Smith",address: "No.1518,  Jinshajiang Road, Putuo District"}
];// 把数据暴露出去供父组件调用
defineExpose({dialogTableVisible
});
</script>

父组件:src/App.vue

<script setup lang="ts">
import BaseInfoDialog from "./components/BaseInfoDialog.vue";
import { ref } from "vue";const childComponentRef = ref(null);const logChildMessage = () => {if (childComponentRef.value) {childComponentRef.value.dialogTableVisible = true;}
};
</script><template><div><div><BaseInfoDialog ref="childComponentRef" /></div><div><el-button type="primary" @click="logChildMessage">open dialog</el-button></div></div>
</template><style scoped></style>

package.json

{"name": "latest-vue3-ts","version": "0.0.0","private": true,"type": "module","scripts": {"dev": "vite","build": "run-p type-check \"build-only {@}\" --","preview": "vite preview","build-only": "vite build","type-check": "vue-tsc --build --force"},"dependencies": {"element-plus": "^2.7.6","vue": "^3.4.29"},"devDependencies": {"@tsconfig/node20": "^20.1.4","@types/node": "^20.14.5","@vitejs/plugin-vue": "^5.0.5","@vue/tsconfig": "^0.5.1","npm-run-all2": "^6.2.0","typescript": "~5.4.0","unplugin-auto-import": "^0.17.6","unplugin-vue-components": "^0.27.0","vite": "^5.3.1","vite-plugin-vue-setup-extend": "^0.4.0","vue-tsc": "^2.0.21"}
}

vite.config.ts

import { fileURLToPath, URL } from 'node:url'import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(),VueSetupExtend(),AutoImport({resolvers: [ElementPlusResolver()],}),Components({resolvers: [ElementPlusResolver()],})],resolve: {alias: {'@': fileURLToPath(new URL('./src', import.meta.url))}}
})

http://www.lryc.cn/news/382360.html

相关文章:

  • 使用STL容器还是Qt容器?
  • Android 2ndBLE的实现
  • 常见硬件工程师面试题(二)
  • java构造方法的重载
  • webpack 压缩图片
  • JAVA每日作业day6.24
  • 鸿蒙开发系统基础能力:【@ohos.hiTraceChain (分布式跟踪)】
  • .git目录解读
  • 如何在Java中处理InterruptedException异常?
  • 深入解读Netty中的NIO:原理、架构与实现详解
  • Vim和Nano简介
  • mysql的information_schema浅析
  • 力扣爆刷第153天之TOP100五连刷26-30(接雨水、环形链表、最长上升子序列)
  • 【Linux】—Apache Hive 安装部署
  • 组装盒示范程序
  • 推荐一款AI修图工具,支持AI去水印,AI重绘,AI抠图...
  • 2024广东省职业技能大赛云计算赛项实战——容器化部署Nginx
  • 压缩pdf文件大小在线,在线免费压缩pdf
  • 薄冰英语语法学习--名词1
  • oracle12c到19c adg搭建(六)切换后12c备库服务器安装19c软件在19c主库升级数据字典后尝试同步
  • Scope XY Project的使用
  • Pytorch Geometric(PyG)入门
  • 大模型KV Cache节省神器MLA学习笔记(包含推理时的矩阵吸收分析)
  • 项目中eventbus和rabbitmq配置后,不起作用
  • 文库小程序搭建部署:实现资源共享正向反馈
  • ONLYOFFICE 桌面编辑器8.1---一个高效且强大的办公软件
  • QThread 与QObject::moveToThread利用Qt事件循环在子线程执行多个函数
  • 6-2 归并排序
  • Java NIO(一) 概述
  • JUC线程池最佳实践