淺談vue2的$refs在vue3組合式API中的替代方法
如果你有過vue2的項目開發(fā)經(jīng)驗,那么對$refs就很熟悉了。由于vue3的斷崖式的升級,在vue3中如何使用$refs呢?想必有遇到過類似的問題,我也有一樣的疑惑。通過搜索引擎和github,基本掌握如何使用$refs。在vue3中使用組合式API的函數(shù)ref來代替靜態(tài)或者動態(tài)html元素的應(yīng)用。
最近業(yè)余在學習vue3項目《蠟筆(Crayon)管理模板:Vue3 + Vuex4 + Ant Design2》開發(fā),這兩天迭代推進了一點,實現(xiàn)了chart圖表組件,寫文章的時候發(fā)現(xiàn)提交代碼的commit有錯別字。
在vue3中使用組合式API的setup()方法的時候,無法正常使用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(); }); // 附加一個實例給refChart refChart.value.instance = chartHelper; }; // 設(shè)置高度 const setHeight = () => { return { height: props.height, }; }; // 綁定一個實例,使用inject注入 const bindInstance = () => { if (props.refKey) { const bind = inject(`bind[${props.refKey}]`); if (bind) { bind(refChart.value); } } }; onMounted(() => { bindInstance(); init(); }); return { refChart, setHeight, }; }, }); </script>
這段代碼完整的實現(xiàn)了一個圖表組件Chart,其中自定義了屬性props,通過把參數(shù)傳遞給setup方法來使用其屬性值。html中定義一個ref="refChart"來作為圖表的dom對象,在setup方法中通過方法ref方法來定義響應(yīng)式可變對象,并在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>
下面的情況,會出現(xiàn)程序錯誤,無法達到預期效果。應(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é)論
本文只是簡單的介紹ref方法的使用,正好在項目中用到,后續(xù)將繼續(xù)邊學邊推進項目并做好筆記。
到此這篇關(guān)于淺談vue2的$refs在vue3組合式API中的替代方法的文章就介紹到這了,更多相關(guān)vue3組合式API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明方法詳解
我在學習vue3+vite+ts的時候,在配置別名這一步的時候遇到了一個問題,這篇文章主要給大家介紹了關(guān)于vite配置別名并處理報錯:找不到模塊“xxx”或其相應(yīng)的類型聲明的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue3使用vue-qrcode-reader實現(xiàn)掃碼綁定設(shè)備功能(推薦)
本文介紹了在Vue3中使用vue-qrcode-reader版本5.5.7來實現(xiàn)移動端的掃碼綁定設(shè)備功能,用戶通過掃描二維碼自動獲取設(shè)備序列號,并填充到添加設(shè)備界面,完成設(shè)備綁定的全過程,包含ScanCode.vue和DeviceAdd.vue兩個主要界面的實現(xiàn)方式2024-10-10el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進行處理)
本文主要介紹了el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進行處理),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03