Vue 3 中 toRaw 的用法詳細(xì)講解
toRaw 是 Vue 3 提供的一個 API,主要用于從 Vue 的響應(yīng)式對象中獲取其對應(yīng)的原始非響應(yīng)式對象。
1. toRaw 的作用
1、獲取原始對象
當(dāng)對象被 Vue 的響應(yīng)式系統(tǒng)包裹時,直接訪問它會返回一個 Proxy 對象。如果需要訪問未被響應(yīng)式系統(tǒng)代理的原始對象,可以使用 toRaw。
2、調(diào)試輔助
在調(diào)試時,如果響應(yīng)式對象出現(xiàn)了意料之外的行為,toRaw 可以幫助我們查看原始數(shù)據(jù)。
3、與第三方庫兼容
某些第三方庫不支持 Vue 的響應(yīng)式對象(Proxy 對象),這時可以通過 toRaw 將響應(yīng)式對象轉(zhuǎn)為普通對象傳遞給它們。
2. toRaw 的用法
2.1 基本語法
import { reactive, toRaw } from 'vue'; const reactiveObj = reactive({ foo: 'bar' }); const rawObj = toRaw(reactiveObj); console.log(reactiveObj); // Proxy {foo: "bar"} console.log(rawObj); // {foo: "bar"}
2.2 使用場景
1、獲取原始對象進(jìn)行對比或操作
響應(yīng)式對象中的 Proxy 包裹可能會導(dǎo)致意外行為,比如在比較對象時。
import { reactive, toRaw } from 'vue'; const reactiveObj = reactive({ a: 1 }); const rawObj = toRaw(reactiveObj); // 比較原始對象 console.log(rawObj === reactiveObj); // false console.log(rawObj == { a: 1 }); // false // 使用原始對象 const anotherObj = { ...rawObj }; // 拷貝原始對象 console.log(anotherObj); // { a: 1 }
2、Debug 或日志記錄
為了避免調(diào)試時輸出 Proxy 對象,可以用 toRaw 獲取原始數(shù)據(jù)。
import { reactive, toRaw } from 'vue'; const state = reactive({ count: 0 }); function logState() { console.log('State:', toRaw(state)); } state.count++; logState(); // 輸出:State: { count: 1 }
3、防止無限遞歸更新
在某些情況下(如遞歸處理響應(yīng)式對象),直接操作響應(yīng)式數(shù)據(jù)可能會導(dǎo)致不必要的額外開銷或無限遞歸,使用 toRaw 可以避免這些問題。
import { reactive, toRaw } from 'vue'; const data = reactive({ nested: { value: 1 } }); function process(obj) { const raw = toRaw(obj); // 獲取原始對象 console.log(raw); // { value: 1 } } process(data.nested);
3. 注意事項
1、toRaw 不會脫離響應(yīng)式系統(tǒng)
使用 toRaw 獲取原始對象后,對原始對象的修改不會觸發(fā) Vue 的響應(yīng)式更新,但對原始對象的修改仍會觸發(fā)更新。
??
<template> <div>obj.foo: {{ obj.foo }}</div> </template> <script setup> import { reactive, toRaw } from 'vue' const obj = reactive({ foo: '更新前數(shù)據(jù) hello' }) const raw = toRaw(obj) setTimeout(() => { raw.foo = '更新后數(shù)據(jù) hi' console.log('obj.foo', obj.foo) // "更新后數(shù)據(jù) hi"(原始對象和響應(yīng)式對象指向同一內(nèi)存地址) }) </script>
2、 不要濫用 toRaw
- toRaw 應(yīng)用于特定場景,如與第三方庫交互或調(diào)試時。一般情況下,應(yīng)盡量使用響應(yīng)式數(shù)據(jù)。
- 濫用 toRaw 可能破壞 Vue 的響應(yīng)式系統(tǒng),導(dǎo)致不可預(yù)測的行為。
3、原始對象不能被 reactive 再次代理
如果對原始對象應(yīng)用 reactive,Vue 會返回其原始的響應(yīng)式對象,而不是重新代理它。
??
import { reactive, toRaw } from 'vue' const obj = reactive({ foo: 'bar' }) const raw = toRaw(obj) const newReactive = reactive(raw) console.log(newReactive === obj) // true
4、只對響應(yīng)式對象有效
如果傳入的不是響應(yīng)式對象,toRaw 會直接返回原對象。
import { toRaw } from 'vue' const plainObj = { foo: 'bar' } console.log(toRaw(plainObj) === plainObj) // true
完整示例 ??
import { defineComponent, reactive, toRaw } from 'vue'; export default defineComponent({ setup() { const state = reactive({ items: [ { id: 1, name: 'Vue' }, { id: 2, name: 'React' }, ], }); const addRawItem = () => { const raw = toRaw(state.items); // 獲取原始數(shù)組 raw.push({ id: raw.length + 1, name: 'Angular' }); console.log('raw', raw); }; return () => ( <div> <h1>技術(shù)棧</h1> <ul> {state.items.map((item) => (<li key={item.id}>{item.name}</li>))} </ul> <button onClick={addRawItem}>添加項</button> </div> ); }, });
展示為:
使用建議:
1、優(yōu)先使用 Vue 的響應(yīng)式系統(tǒng),toRaw 只在特殊場景中使用。
2、??:注意原始對象的修改不會觸發(fā)視圖更新。
3、避免過度依賴 toRaw,以免破壞響應(yīng)式的優(yōu)勢。
到此這篇關(guān)于Vue 3 中 toRaw 的詳細(xì)講解的文章就介紹到這了,更多相關(guān)Vue 3 toRaw 的詳細(xì)講解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在vue中使用export?default導(dǎo)出的class類方式
這篇文章主要介紹了在vue中使用export?default導(dǎo)出的class類方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Vue使用JsonView進(jìn)行JSON數(shù)據(jù)展示
Vue-JSON-Viewer 是一個用于在Vue項目中展示JSON數(shù)據(jù)的組件,它解決了在項目開發(fā)中面臨的展示JSON數(shù)據(jù)的需求,下面就跟隨小編一起來了解下它的具體使用吧2025-03-03vue實現(xiàn)多組關(guān)鍵詞對應(yīng)高亮顯示功能
最近小編遇到這樣的問題,多組關(guān)鍵詞,這里實現(xiàn)了關(guān)鍵詞的背景色與匹配值的字體顏色值相同,下面通過定義關(guān)鍵詞匹配改變字體顏色,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2019-07-07Elementui?el-input輸入框校驗及表單校驗實例代碼
輸入框是使用非常多的元素,用來輸入用戶名、密碼等等信息,Element提供了功能和樣式豐富的輸入框,下面這篇文章主要給大家介紹了關(guān)于Elementui?el-input輸入框校驗及表單校驗的相關(guān)資料,需要的朋友可以參考下2023-06-06vuex提交state&&實時監(jiān)聽state數(shù)據(jù)的改變方法
今天小編就為大家分享一篇vuex提交state&&實時監(jiān)聽state數(shù)據(jù)的改變方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09