vue3+ts中ref與reactive指定類型實(shí)現(xiàn)示例
ref 的基礎(chǔ)特性
ref 約等于 reactive({ value: x })
ref() 可以定義時(shí)無(wú)參數(shù),第一次賦值任意類型,然后就不能增加屬性
const refa = ref(6) const rcta = reactive({ value: 12 }) console.log('refa:', refa) //RefImpl{...} console.log('refa:', refa.value) //6 console.log('rcta:', rcta) //Proxy {value: 12} console.log('rcta.value:', rcta.value) //12 const refb = ref({ name: 'bbb' }) console.log('refb:', refb.value, refb.value.name) //Proxy{name: 'bbb'} bbb //refb.value.age=18 //報(bào)錯(cuò) //類型{ name: string;}上不存在屬性age
如何在ref中指定類型
const a = ref('') //根據(jù)輸入?yún)?shù)推導(dǎo)字符串類型 Ref<string> const b = ref<string[]>([]) //可以通過(guò)范型顯示約束 Ref<string[]> const c: Ref<string[]> = ref([]) //聲明類型 Ref<string[]> const list = ref([1, 3, 5]) console.log('list前:', list.value) list.value[1] = 7 console.log('list后:', list.value) type typPeople = { name: string age: number } const list2: Ref<typPeople[]> = ref([]) console.log('list2-前:', list2.value) //{} 不是空數(shù)組,而是空對(duì)象 list2.value.push({ name: '小張', age: 18 }) console.log('list2-后:', list2.value[0]) //{name: '小張', age: 18} ********* ref 內(nèi)部值指定類型 ********* const foo = ref<string | number>('foo') foo.value = 123 ********* 如果ref類型未知,則建議將 ref 轉(zhuǎn)換為 Ref<T>: ********* function useState<T>(initial: T) { const state = ref(initial) as Ref<T> return state } const item: typPeople = { name: 'aa', age: 18 } const x1 = useState(item) // x1 類型為: Ref<typPeople> const x2 = ref(item) //x2 類型為: Ref<{ name:string; age: number;}> console.log('ref.value:', x1.value, x1.value.name) //Proxy{name: 'aa', age: 18} aa
reactive
返回對(duì)象的響應(yīng)式副本
reactive(x) 必須要指定參數(shù),所以類型就已經(jīng)確定了,也不能增加屬性
const count = ref(1) console.log('ref:', count) //RefImpl{...} //當(dāng)ref分配給reactive時(shí),ref將自動(dòng)解包 const obj = reactive({ a: count }) //不需要count.value console.log(obj.a) // 1 console.log(obj.a === count.value) // true //obj.b=7 //添加屬性會(huì)報(bào)錯(cuò) // { a: number; }上不存在屬性b //const str=reactive("aaa") //這是報(bào)錯(cuò)的,reactive參數(shù)只能是對(duì)象 const arr = reactive([1, 2]) //數(shù)組,其實(shí)結(jié)果還是對(duì)象 const obj = reactive({ 0: 1, 1: 2 }) console.log('arr', arr) //Proxy {0: 1, 1: 2} console.log('obj', obj) //Proxy {0: 1, 1: 2} //reactive定義和ref不同,ref返回的是Ref<T>類型,reactive不存在Reactive<T> //它返回是UnwrapNestedRefs<T>,和傳入目標(biāo)類型一致,所以不存在定義通用reactive類型 function reactiveFun<T extends object>(target: T) { const state = reactive(target) as UnwrapNestedRefs<T> return state } type typPeople = { name: string age: number } const item: typPeople = { name: 'aa', age: 18 } const obj1 = reactive(item) //obj1 類型為: { name: string; age: number; } const obj2 = reactiveFun(item) //obj2 類型為: { name: string; age: number; }
isRef、isReactive
// isRef 檢查值是否為一個(gè) ref 對(duì)象 console.log('是否為ref:', isRef(count)) //true //isProxy 檢查對(duì)象是否是 由reactive或readonly創(chuàng)建的 proxy //readonly是原始對(duì)象的只讀代理 console.log('ref是否為proxy:', isProxy(count)) //false console.log('reactive是否為proxy:', isProxy(obj)) //true //isReactive 檢查對(duì)象是否是由 reactive 創(chuàng)建的響應(yīng)式代理 console.log('isReactive判斷:', isReactive(obj)) //true
toRef、toRefs、toRaw
- toRef 為源響應(yīng)式對(duì)象上的某個(gè)元素 新創(chuàng)建一個(gè) ref
- toRefs 將響應(yīng)式對(duì)象Proxy 轉(zhuǎn)換為普通對(duì)象,且元素都指向原始對(duì)象的ref
- toRaw 返回 reactive或readonly代理的原始對(duì)象
toRef 當(dāng)你要將 prop 的 ref 傳遞給復(fù)合函數(shù)時(shí),toRef 很有用
const state = reactive({ foo: 1, bar: 2 }) console.log(state.foo) //1 state.foo++ console.log(state.foo) //2 const fooRef = toRef(state, 'foo') fooRef.value++ console.log(state.foo) //3 但state.foo并沒(méi)有.value屬性,不要混淆
toRefs 將響應(yīng)式對(duì)象Proxy轉(zhuǎn)換為普通對(duì)象,且元素都指向原始對(duì)象的ref
toRaw 返回 reactive或readonly 代理的原始對(duì)象,當(dāng)然也可以返回ref的原始對(duì)象
const state = reactive({ foo: 1, bar: 2 }) console.log(state) //Proxy {foo: 1, bar: 2} const refs1 = toRefs(state) //toRefs 將響應(yīng)式對(duì)象Proxy 轉(zhuǎn)換為普通對(duì)象 console.log('toRefs:', refs1) //{foo: ObjectRefImpl, bar: ObjectRefImpl} const refs2 = toRaw(state) //toRaw 返回 reactive或readonly代理的原始對(duì)象 console.log('toRaw:', refs2) //{foo: 1, bar: 2} const ref1 = ref(5) //ref并不是Proxy,而是RefImpl const refs3 = toRefs(ref1) //不報(bào)錯(cuò),但沒(méi)意義
以上就是vue3+ts中ref及reactive指定類型實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于vue3+ts實(shí)現(xiàn)ref及reactive指定類型的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue el-table 動(dòng)態(tài)添加行與刪除行的實(shí)現(xiàn)
這篇文章主要介紹了vue el-table 動(dòng)態(tài)添加行與刪除行的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vite創(chuàng)建一個(gè)標(biāo)準(zhǔn)vue3+ts+pinia項(xiàng)目
本文主要介紹了vite創(chuàng)建一個(gè)標(biāo)準(zhǔn)vue3+ts+pinia項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05VUE項(xiàng)目實(shí)現(xiàn)主題切換的多種方法
這篇文章主要介紹了VUE項(xiàng)目實(shí)現(xiàn)主題切換的方法,本文通過(guò)多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11vue+element使用動(dòng)態(tài)加載路由方式實(shí)現(xiàn)三級(jí)菜單頁(yè)面顯示的操作
這篇文章主要介紹了vue+element使用動(dòng)態(tài)加載路由方式實(shí)現(xiàn)三級(jí)菜單頁(yè)面顯示的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08vue-router報(bào)錯(cuò):uncaught error during route 
這篇文章主要介紹了vue-router報(bào)錯(cuò):uncaught error during route navigati問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決
這篇文章主要介紹了vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue實(shí)現(xiàn)公共組件傳值并及時(shí)監(jiān)聽(tīng)到數(shù)據(jù)更新視圖
這篇文章主要介紹了vue實(shí)現(xiàn)公共組件傳值并及時(shí)監(jiān)聽(tīng)到數(shù)據(jù)更新視圖方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue.js 雙層嵌套for遍歷的方法詳解, 類似php foreach()
今天小編就為大家分享一篇vue.js 雙層嵌套for遍歷的方法詳解, 類似php foreach(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue中promise的使用及異步請(qǐng)求數(shù)據(jù)的方法
這篇文章主要介紹了vue中promise的使用及異步請(qǐng)求數(shù)據(jù)的方法,文章給大家較詳細(xì)的介紹了遇到的問(wèn)題及解決方法,需要的朋友可以參考下2018-11-11