vue3響應(yīng)式轉(zhuǎn)換常用API操作示例代碼
響應(yīng)式常用API
- ref 相關(guān):toRef、toRefs、unRef
- 只讀代理:readonly
- 判斷相關(guān):isRef、isReactive、isProxy、isReadonly
- 3.3新增API:toValue
ref相關(guān)
toRef:基于響應(yīng)式對象的某一個(gè)屬性,將其轉(zhuǎn)換為 ref 值
import { reactive, toRef } from 'vue'
const state = reactive({
count: 0
})
const countRef = toRef(state, 'count')
// 這里其實(shí)就等價(jià)于 ref(state.count)
console.log(countRef)
console.log(countRef.value)import { reactive, isReactive, toRef } from 'vue'
const state = reactive({
count: {
value: 0
}
})
console.log(isReactive(state)) // true
console.log(isReactive(state.count)) // true
const countRef = toRef(state, 'count')
// 相當(dāng)于 ref(state.count)
console.log(countRef)
console.log(countRef.value)
console.log(countRef.value.value)toRefs:將一個(gè)響應(yīng)式對象轉(zhuǎn)為一個(gè)普通對象,普通對象的每一個(gè)屬性對應(yīng)的是一個(gè) ref 值
import { reactive, toRefs } from 'vue'
const state = reactive({
count: 0,
message: 'hello'
})
const stateRefs = toRefs(state)
console.log(stateRefs) // {count: RefImpl, message: RefImpl}
console.log(stateRefs.count.value)
console.log(stateRefs.message.value)unRef: 如果參數(shù)給的是一個(gè) ref 值,那么就返回內(nèi)部的值,如果不是 ref,那么就返回參數(shù)本身
這個(gè) API 實(shí)際上是一個(gè)語法糖: val = isRef(val) ? val.value : val
import { ref, unref } from 'vue'
const countRef = ref(10)
const normalValue = 20
console.log(unref(countRef)) // 10
console.log(unref(normalValue)) // 20只讀代理
接收一個(gè)對象(不論是響應(yīng)式的還是普通的)或者一個(gè) ref,返回一個(gè)原來值的只讀代理。
import { ref, readonly } from 'vue'
const count = ref(0)
const count2 = readonly(count) // 相當(dāng)于創(chuàng)建了一個(gè) count 的只讀版本
count.value++;
count2.value++; // 會給出警告在某些場景下,我們就是希望一些數(shù)據(jù)只能讀取不能修改
const rawConfig = {
apiEndpoint: 'https://api.example.com',
timeout: 5000
};
// 例如在這個(gè)場景下,我們就期望這個(gè)配置對象是不能夠修改的
const config = readonly(rawConfig)判斷相關(guān)
isRef 和 isReactive
import { ref, shallowRef, reactive, shallowReactive, isRef, isReactive } from 'vue'
const obj = {
a:1,
b:2,
c: {
d:3,
e:4
}
}
const state1 = ref(obj)
const state2 = shallowRef(obj)
const state3 = reactive(obj)
const state4 = shallowReactive(obj)
console.log(isRef(state1)) // true
console.log(isRef(state2)) // true
console.log(isRef(state1.value.c)) // false
console.log(isRef(state2.value.c)) // false
console.log(isReactive(state1.value.c)) // true
console.log(isReactive(state2.value.c)) // false
console.log(isReactive(state3)) // true
console.log(isReactive(state4)) // true
console.log(isReactive(state3.c)) // true
console.log(isReactive(state4.c)) // falseisProxy: 檢查一個(gè)對象是否由 reactive、readonly、shallowReactive、shallowReadonly 創(chuàng)建的代理
import { reactive, readonly, shallowReactive, shallowReadonly, isProxy } from 'vue'
// 創(chuàng)建 reactive 代理對象
const reactiveObject = reactive({ message: 'Hello' })
// 創(chuàng)建 readonly 代理對象
const readonlyObject = readonly({ message: 'Hello' })
// 創(chuàng)建 shallowReactive 代理對象
const shallowReactiveObject = shallowReactive({ message: 'Hello' })
// 創(chuàng)建 shallowReadonly 代理對象
const shallowReadonlyObject = shallowReadonly({ message: 'Hello' })
// 創(chuàng)建普通對象
const normalObject = { message: 'Hello' }
console.log(isProxy(reactiveObject)) // true
console.log(isProxy(readonlyObject)) // true
console.log(isProxy(shallowReactiveObject)) // true
console.log(isProxy(shallowReadonlyObject)) // true
console.log(isProxy(normalObject)) // false3.3新增API
toValue
這個(gè) API 和前面介紹的 unref 比較相似
import { ref, toValue } from 'vue'
const countRef = ref(10)
const normalValue = 20
console.log(toValue(countRef)) // 10
console.log(toValue(normalValue)) // 20toValue 相比 unref 更加靈活一些,它支持傳入 getter 函數(shù),并且返回函數(shù)的執(zhí)行結(jié)果
import { ref, toValue } from 'vue'
const countRef = ref(10)
const normalValue = 20
const getter = ()=>30;
console.log(toValue(countRef)) // 10
console.log(toValue(normalValue)) // 20
console.log(toValue(getter)) // 30-EOF-
到此這篇關(guān)于vue3響應(yīng)式轉(zhuǎn)換常用API的文章就介紹到這了,更多相關(guān)vue3響應(yīng)式常用API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何正確解決VuePress本地訪問出現(xiàn)資源報(bào)錯(cuò)404的問題
這篇文章主要介紹了如何正確解決VuePress本地訪問出現(xiàn)資源報(bào)錯(cuò)404的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼
本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Vue.use與Vue.prototype的區(qū)別及說明
這篇文章主要介紹了Vue.use與Vue.prototype的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
uni-app中使用ECharts配置四種不同的圖表(示例詳解)
在uni-app中集成ECharts可以為我們的應(yīng)用提供強(qiáng)大的圖表功能,我們詳細(xì)說一下如何在uni-app中使用ECharts,并配置四種不同的圖表,感興趣的朋友跟隨小編一起看看吧2024-01-01
Vue.js3.2響應(yīng)式部分的優(yōu)化升級詳解
這篇文章主要為大家介紹了Vue.js3.2響應(yīng)式部分的優(yōu)化升級詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue3中vite.config.js相關(guān)常用配置超詳細(xì)講解
在Vue3項(xiàng)目中vite.config.js文件是Vite構(gòu)建工具的配置文件,它用于定義項(xiàng)目的構(gòu)建和開發(fā)選項(xiàng),這篇文章主要介紹了vue3中vite.config.js相關(guān)常用配置的相關(guān)資料,需要的朋友可以參考下2025-04-04

