vue3使用ref的性能警告問(wèn)題
vue3使用ref的性能警告
問(wèn)題
使用 ref 的性能警告 代碼如下
<template> <div> <component :is="currentTabComponent"></component> </div> </template> <script setup> import { ref,shallowRef } from "vue"; import TodoList from "./components/TodoList.vue"; import Rate from "./components/Rate.vue"; let tabs ={ TodoList, Rate } let currentTabComponent = ref(TodoList) </script>
警告
runtime-core.esm-bundler.js:6591 [Vue warn]: Vue received a Component which was made a reactive object. This can lead to unnecessary performance overhead, and should be avoided by marking the component with markRaw or using shallowRef instead of ref. Component that was made reactive:
譯文:
runtime-core.esm-bundler.js:6591 [Vue 警告]:Vue 收到一個(gè)組件,該組件已成為響應(yīng)式對(duì)象。這會(huì)導(dǎo)致不必要的性能開銷,應(yīng)該通過(guò)使用 markRaw 標(biāo)記組件或使用 shallowRef 代替 ref 來(lái)避免。被響應(yīng)的組件:
markRaw
: 標(biāo)記一個(gè)對(duì)象,使其永遠(yuǎn)不會(huì)轉(zhuǎn)換為 proxy。返回對(duì)象本身。shallowRef
: 創(chuàng)建一個(gè)跟蹤自身 .value 變化的 ref,但不會(huì)使其值也變成響應(yīng)式的。
解決
我通過(guò)將對(duì)象標(biāo)記為shallowRef解決了這個(gè)問(wèn)題
因此,不要將組件存儲(chǔ)在您的狀態(tài)中,而是存儲(chǔ)對(duì)它的鍵控引用,并針對(duì)對(duì)象進(jìn)行查找
完整代碼
<template> <div> <h1>帶動(dòng)畫的Todolist</h1> <button v-for="(i,tab) in tabs" :key="i" :class="['tab-button', { active: currentTabComponent === tab }]" @click="fn(tab)" > {{ tab }} </button> <component :is="currentTabComponent"></component> </div> </template> <script setup> import { ref,shallowRef } from "vue"; import TodoList from "./components/TodoList.vue"; import Rate from "./components/Rate.vue"; let tabs ={ TodoList, Rate } let currentTabComponent = shallowRef(TodoList) function fn (tab){ currentTabComponent.value = tabs[tab] } </script>
vue3 ref函數(shù)用法
1.在setup函數(shù)中,可以使用ref函數(shù),用于創(chuàng)建一個(gè)響應(yīng)式數(shù)據(jù),當(dāng)數(shù)據(jù)發(fā)生改變時(shí),Vue會(huì)自動(dòng)更新UI
<template> ? ? <div> ? ? ? ? <h1>{{mycount}}</h1> ? ? ? ? <button @click="changeMyCount">changeMyCount</button> ? ? </div> </template> ? <script> import { ref } from "vue"; export default { ? ? name: "ref", ? ? setup(){ ? ? ? ? const mycount = ref(2); ? ? ? ? const changeMyCount = ()=>{ ? ? ? ? ? ? mycount.value = mycount.value + 2 ; ? ? ? ? } ? ? ? ?? ? ? ? ? return { ? ? ? ? ? ? mycount, ? ? ? ? ? ? changeMyCount, ? ? ? ? } ? ? } }; </script>
ref函數(shù)僅能監(jiān)聽基本類型的變化,不能監(jiān)聽復(fù)雜類型的變化(比如對(duì)象、數(shù)組)
監(jiān)聽復(fù)雜類型的變化可以使用reactive函數(shù)
2.通過(guò)ref屬性獲取元素
vue3需要借助生命周期方法,在setup執(zhí)行時(shí),template中的元素還沒掛載到頁(yè)面上,所以必須在mounted之后才能獲取到元素。
<template> ? ? <div> ? ? ? ? <div ref="box"><button>hehe</button></div> ? ? </div> </template> ? <script> import { ref } from "vue"; export default { ? ? name: "ref", ? ? setup(){ ? ? ? ? const box = ref(null) ? ? ? ? onMounted(()=>{ ? ? ? ? ? ? console.log(box.value) ? ? ? ? }) ? ? } }; </script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Unity webgl 嵌入Vue實(shí)現(xiàn)過(guò)程
Unity webgl嵌入到前端網(wǎng)頁(yè)中,前端通過(guò)調(diào)用Unity webgl內(nèi)方法實(shí)現(xiàn)需要展示的功能,前端點(diǎn)擊Unity webgl內(nèi)的交互點(diǎn),Unity webgl返回給前端一些需要的數(shù)據(jù),這篇文章主要介紹了Unity webgl 嵌入Vue實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2024-01-01vue/cli?配置動(dòng)態(tài)代理無(wú)需重啟服務(wù)的操作方法
vue-cli是vue.js的腳手架,用于自動(dòng)生成vue.js+webpack的項(xiàng)目模板,分為vue?init?webpack-simple?項(xiàng)目名和vue?init?webpack?項(xiàng)目名兩種,這篇文章主要介紹了vue/cli?配置動(dòng)態(tài)代理,無(wú)需重啟服務(wù),需要的朋友可以參考下2022-05-05iview table render集成switch開關(guān)的實(shí)例
下面小編就為大家分享一篇iview table render集成switch開關(guān)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Vue 實(shí)現(xiàn)從文件中獲取文本信息的方法詳解
這篇文章主要介紹了Vue 實(shí)現(xiàn)從文件中獲取文本信息的方法,結(jié)合實(shí)例形式詳細(xì)分析了vue.js基于export導(dǎo)出的文件信息讀取相關(guān)操作技巧,需要的朋友可以參考下2019-10-10vue項(xiàng)目刷新當(dāng)前頁(yè)面的三種方法
這篇文章主要介紹了vue項(xiàng)目刷新當(dāng)前頁(yè)面的三種方法,本文圖文并茂給大家介紹的非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12vue.js實(shí)現(xiàn)選項(xiàng)卡切換
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)選項(xiàng)卡切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03