vue3使用ref 獲取不到子組件屬性問題的解決辦法
需求:
父子組件使用<script setup>語法糖,父組件通過給子組件定義ref訪問子組件內(nèi)部屬性或事件。
關(guān)鍵點:
子組件中,setup語法糖需要用defineExpose把要讀取的屬性和方法單獨暴露出去,否則會訪問失??;如果子組件使用setup()函數(shù),則在父組件通過ref可以直接訪問其屬性,不需要用defineExpose暴露數(shù)據(jù)。
子組件: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" } ]; // 把數(shù)據(jù)暴露出去供父組件調(diào)用 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)) } } })
到此這篇關(guān)于vue3使用ref 獲取不到子組件屬性問題的解決辦法的文章就介紹到這了,更多相關(guān)vue3 ref子組件屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?url跳轉(zhuǎn)解析和參數(shù)編碼介紹
這篇文章主要介紹了vue?url跳轉(zhuǎn)解析和參數(shù)編碼,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03詳解Vue如何進行表單聯(lián)動與級聯(lián)選擇
表單聯(lián)動和級聯(lián)選擇是Vue.js中常見的功能,在下面的文章中,我們將討論如何在Vue.js中實現(xiàn)表單聯(lián)動和級聯(lián)選擇,感興趣的小伙伴可以了解一下2023-06-06vue實現(xiàn)拖拽的簡單案例 不超出可視區(qū)域
這篇文章主要為大家詳細介紹了vue實現(xiàn)拖拽的簡單案例,不超出可視區(qū)域,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07