vue3使用ref的性能警告問題
vue3使用ref的性能警告
問題
使用 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)該通過使用 markRaw 標(biāo)記組件或使用 shallowRef 代替 ref 來避免。被響應(yīng)的組件:
markRaw: 標(biāo)記一個(gè)對(duì)象,使其永遠(yuǎn)不會(huì)轉(zhuǎn)換為 proxy。返回對(duì)象本身。shallowRef: 創(chuàng)建一個(gè)跟蹤自身 .value 變化的 ref,但不會(huì)使其值也變成響應(yīng)式的。
解決
我通過將對(duì)象標(biāo)記為shallowRef解決了這個(gè)問題
因此,不要將組件存儲(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.通過ref屬性獲取元素
vue3需要借助生命周期方法,在setup執(zhí)行時(shí),template中的元素還沒掛載到頁面上,所以必須在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)文章
vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式(推薦)
這篇文章主要介紹了vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式,本文通過實(shí)例代碼給大家介紹的分需要的朋友可以參考下2024-09-09
vue項(xiàng)目實(shí)現(xiàn)按鈕可隨意移動(dòng)
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)按鈕可隨意移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue.js如何利用v-for循環(huán)生成動(dòng)態(tài)標(biāo)簽
這篇文章主要給大家介紹了關(guān)于Vue.js如何利用v-for循環(huán)生成動(dòng)態(tài)標(biāo)簽的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Vue3源碼分析reactivity實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了Vue3源碼分析reactivity實(shí)現(xiàn)方法原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
vue3學(xué)習(xí)指導(dǎo)教程(附帶獲取屏幕可視區(qū)域?qū)捀?
Vue3是Vue的第三個(gè)版本更快,更輕,易維護(hù),更多的原生支持,下面這篇文章主要給大家介紹了關(guān)于vue3學(xué)習(xí)指導(dǎo)教程(附帶獲取屏幕可視區(qū)域?qū)捀?的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
利用Vue實(shí)現(xiàn)卡牌翻轉(zhuǎn)的特效
這篇文章主要介紹了如何利用Vue實(shí)現(xiàn)一個(gè)春節(jié)抽??撁?,采用了卡牌翻轉(zhuǎn)的形式。文中的實(shí)現(xiàn)方法講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-02-02
使用Vue3實(shí)現(xiàn)一個(gè)穿梭框效果的示例代碼
這篇文章主要給大家介紹了如何使用?Vue3?實(shí)現(xiàn)一個(gè)穿梭框效果,當(dāng)選中數(shù)據(jù),并且點(diǎn)擊相對(duì)應(yīng)的方向箭頭時(shí),選中的數(shù)據(jù)會(huì)發(fā)送到對(duì)面,并且數(shù)據(jù)會(huì)保持正確的順序進(jìn)行排列,文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12

