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