淺談vue2的$refs在vue3組合式API中的替代方法
如果你有過(guò)vue2的項(xiàng)目開發(fā)經(jīng)驗(yàn),那么對(duì)$refs就很熟悉了。由于vue3的斷崖式的升級(jí),在vue3中如何使用$refs呢?想必有遇到過(guò)類似的問(wèn)題,我也有一樣的疑惑。通過(guò)搜索引擎和github,基本掌握如何使用$refs。在vue3中使用組合式API的函數(shù)ref來(lái)代替靜態(tài)或者動(dòng)態(tài)html元素的應(yīng)用。
最近業(yè)余在學(xué)習(xí)vue3項(xiàng)目《蠟筆(Crayon)管理模板:Vue3 + Vuex4 + Ant Design2》開發(fā),這兩天迭代推進(jìn)了一點(diǎn),實(shí)現(xiàn)了chart圖表組件,寫文章的時(shí)候發(fā)現(xiàn)提交代碼的commit有錯(cuò)別字。

在vue3中使用組合式API的setup()方法的時(shí)候,無(wú)法正常使用this.$refs,但可以使用新的函數(shù)ref()。
下面代碼摘自:https://github.com/QuintionTang/crayon/blob/feat-dashboard/src/qtui/components/Chart.vue
<template>
<canvas ref="refChart" :height="setHeight"></canvas>
</template>
<script>
import { defineComponent, onMounted, ref, inject, watch } from "vue";
import Chart from "chart.js";
import { deepCopy } from "@/helper/index";
export default defineComponent({
name: "QtChart",
props: {
type: {
type: String,
required: true,
default: "line",
},
data: {
type: Object,
required: true,
default: () => ({}),
},
options: {
type: Object,
default: () => ({}),
},
height: {
type: Number,
default: 0,
},
refKey: {
type: String,
default: null,
},
},
setup(props) {
const refChart = ref();
// 初始化方法
const init = () => {
const canvasChart = refChart.value?.getContext("2d");
const chartHelper = new Chart(canvasChart, {
type: props.type,
data: deepCopy(props.data),
options: props.options,
});
watch(props, () => {
chartHelper.data = deepCopy(props.data);
chartHelper.options = props.options;
chartHelper.update();
});
// 附加一個(gè)實(shí)例給refChart
refChart.value.instance = chartHelper;
};
// 設(shè)置高度
const setHeight = () => {
return {
height: props.height,
};
};
// 綁定一個(gè)實(shí)例,使用inject注入
const bindInstance = () => {
if (props.refKey) {
const bind = inject(`bind[${props.refKey}]`);
if (bind) {
bind(refChart.value);
}
}
};
onMounted(() => {
bindInstance();
init();
});
return {
refChart,
setHeight,
};
},
});
</script>
這段代碼完整的實(shí)現(xiàn)了一個(gè)圖表組件Chart,其中自定義了屬性props,通過(guò)把參數(shù)傳遞給setup方法來(lái)使用其屬性值。html中定義一個(gè)ref="refChart"來(lái)作為圖表的dom對(duì)象,在setup方法中通過(guò)方法ref方法來(lái)定義響應(yīng)式可變對(duì)象,并在setup函數(shù)結(jié)尾作為返回值。
const refChart = ref();
需要注意的是,返回值的屬性名必須和html中的ref值一致。
下面代碼是可以正常執(zhí)行的。
<template>
<canvas ref="refChart" :height="setHeight"></canvas>
</template>
<script>
import { defineComponent, onMounted, ref, inject, watch } from "vue";
import Chart from "chart.js";
import { deepCopy } from "@/helper/index";
export default defineComponent({
name: "QtChart",
props: {
// ...
},
setup(props) {
const refChartBox = ref();
// ...
return {
refChart:refChartBox,
};
},
});
</script>
下面的情況,會(huì)出現(xiàn)程序錯(cuò)誤,無(wú)法達(dá)到預(yù)期效果。應(yīng)為html中定義的ref="refChart"和setup返回的refChartBox不一致。
<template>
<canvas ref="refChart" :height="setHeight"></canvas>
</template>
<script>
import { defineComponent, onMounted, ref, inject, watch } from "vue";
import Chart from "chart.js";
import { deepCopy } from "@/helper/index";
export default defineComponent({
name: "QtChart",
props: {
// ...
},
setup(props) {
const refChartBox = ref();
// ...
return {
refChartBox,
};
},
});
</script>
結(jié)論
本文只是簡(jiǎn)單的介紹ref方法的使用,正好在項(xiàng)目中用到,后續(xù)將繼續(xù)邊學(xué)邊推進(jìn)項(xiàng)目并做好筆記。
到此這篇關(guān)于淺談vue2的$refs在vue3組合式API中的替代方法的文章就介紹到這了,更多相關(guān)vue3組合式API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決ElementUI中tooltip出現(xiàn)無(wú)法顯示的問(wèn)題
這篇文章主要介紹了解決ElementUI中tooltip出現(xiàn)無(wú)法顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
淺談Vue CLI 3結(jié)合Lerna進(jìn)行UI框架設(shè)計(jì)
這篇文章主要介紹了淺談Vue CLI 3結(jié)合Lerna進(jìn)行UI框架設(shè)計(jì),在此之前先簡(jiǎn)單介紹一下Element的構(gòu)建流程,以便對(duì)比新的UI框架設(shè)計(jì)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
vue 實(shí)現(xiàn)網(wǎng)頁(yè)截圖功能詳解
這篇文章主要介紹了通過(guò)vue實(shí)現(xiàn)網(wǎng)頁(yè)截圖的功能,有興趣的童鞋可以了解一下2021-11-11
Vue后臺(tái)管理系統(tǒng)之實(shí)現(xiàn)分頁(yè)功能示例
本文主要介紹了Vue后臺(tái)管理系統(tǒng)之實(shí)現(xiàn)分頁(yè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Vue OptionsAPI與CompositionAPI的區(qū)別與使用介紹
OptionsAPI和CompositionAPI是Vue.js框架中兩種不同的組件編寫方式,OptionsAPI通過(guò)對(duì)象字面量定義組件,以屬性分隔不同功能,響應(yīng)式數(shù)據(jù)通過(guò)data屬性定義,本文給大家介紹Vue OptionsAPI與CompositionAPI的區(qū)別,感興趣的朋友一起看看吧2024-10-10
Vue中computed和watch的區(qū)別小結(jié)
watch和computed都是以Vue的依賴追蹤機(jī)制為基礎(chǔ)的,當(dāng)某一個(gè)依賴型數(shù)據(jù)發(fā)生變化的時(shí)候,所有依賴這個(gè)數(shù)據(jù)的相關(guān)數(shù)據(jù)會(huì)自動(dòng)發(fā)生變化,即自動(dòng)調(diào)用相關(guān)的函數(shù),來(lái)實(shí)現(xiàn)數(shù)據(jù)的變動(dòng),這篇文章簡(jiǎn)單介紹下Vue中computed和watch的區(qū)別異同,感興趣的朋友一起看看吧2022-12-12
Vue.js的動(dòng)態(tài)組件模板的實(shí)現(xiàn)
這篇文章主要介紹了Vue.js的動(dòng)態(tài)組件模板的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
從零搭建一個(gè)vite+vue3+ts規(guī)范基礎(chǔ)項(xiàng)目(搭建過(guò)程問(wèn)題小結(jié))
這篇文章主要介紹了從零搭建一個(gè)vite+vue3+ts規(guī)范基礎(chǔ)項(xiàng)目,本項(xiàng)目已vite開始,所以按照vite官方的命令開始,對(duì)vite+vue3+ts項(xiàng)目搭建過(guò)程感興趣的朋友一起看看吧2022-05-05

