Vue3?setup中使用$refs的方法詳解
在 Vue 3 中的 Composition API 中, $refs 并不直接可用于 setup 函數(shù)。這是因為 $refs 是 Vue 2 的實例屬性,而在 Vue 3 中, setup 函數(shù)是與模板實例分離的,不再使用實例屬性。
實際工作中確實有需求,在 setup 函數(shù)使用$refs,下面有兩種方法。
方案
方案一:getCurrentInstance
<template>
<el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" />
<el-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</el-table-column>
<el-table-column property="name" label="Name" width="120" />
<el-table-column property="address" label="Address" show-overflow-tooltip />
</el-table>
<div style="margin-top: 20px">
<el-button @click="toggleSelection([tableData[1], tableData[2]])">Toggle selection status of second and third
rows</el-button>
<el-button @click="toggleSelection()">Clear selection</el-button>
</div>
</template>
<script setup>
import { ref, getCurrentInstance } from 'vue'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-08',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-06',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-07',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
multipleSelection.value = val
}
const {proxy} = getCurrentInstance();
const toggleSelection = (rows) => {
if (rows) {
rows.forEach((row) => {
proxy.$refs.multipleTableRef.toggleRowSelection(row, undefined)
})
} else {
proxy.$refs.multipleTableRef.clearSelection()
}
}
</script>方案二: ref
<template>
<el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" />
<el-table-column label="Date" width="120">
<template #default="scope">{{ scope.row.date }}</template>
</el-table-column>
<el-table-column property="name" label="Name" width="120" />
<el-table-column property="address" label="Address" show-overflow-tooltip />
</el-table>
<div style="margin-top: 20px">
<el-button @click="toggleSelection([tableData[1], tableData[2]])">Toggle selection status of second and third
rows</el-button>
<el-button @click="toggleSelection()">Clear selection</el-button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-08',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-06',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-07',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
const multipleTableRef = ref()
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
multipleSelection.value = val
}
const toggleSelection = (rows) => {
if (rows) {
rows.forEach((row) => {
multipleTableRef.value.toggleRowSelection(row, undefined)
})
} else {
multipleTableRef.value.clearSelection()
}
}
</script>在這個示例中,multipleTableRef 是一個通過 ref 創(chuàng)建的響應式變量,用于存儲對 el-table 元素的引用。
結(jié)果顯示

到此這篇關于Vue3 setup中使用$refs的方法詳解的文章就介紹到這了,更多相關Vue3 setup內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Ant Design Vue table組件如何自定義分頁器
這篇文章主要介紹了Ant Design Vue table組件如何自定義分頁器問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
使用vue實現(xiàn)多規(guī)格選擇實例(SKU)
這篇文章主要介紹了使用vue實現(xiàn)多規(guī)格選擇實例(SKU),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
vue3 Element Plus中icon圖標不顯示的解決方案
這篇文章主要介紹了vue3 Element Plus中icon圖標不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動端,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07
Vue Router4與Router3路由配置與區(qū)別說明
這篇文章主要介紹了Vue Router4與Router3路由配置與區(qū)別說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-12-12
vue調(diào)試工具vue-devtools安裝及使用方法
本文主要介紹 vue的調(diào)試工具 vue-devtools 的安裝和使用,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-11-11

