vue3+ts中ref與reactive指定類型實(shí)現(xiàn)示例
ref 的基礎(chǔ)特性
ref 約等于 reactive({ value: x })
ref() 可以定義時(shí)無參數(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[]>([]) //可以通過范型顯示約束 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并沒有.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ò),但沒意義以上就是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)文章
前端低代碼form-generator實(shí)現(xiàn)及新增自定義組件詳解
這篇文章主要給大家介紹了關(guān)于前端低代碼form-generator實(shí)現(xiàn)及新增自定義組件的相關(guān)資料,form-generator是一個(gè)開源的表單生成器,可以幫助我們快速構(gòu)建各種表單頁面,需要的朋友可以參考下2023-11-11
element-ui 限制日期選擇的方法(datepicker)
本篇文章主要介紹了element-ui 限制日期選擇的方法(datepicker),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
如何用VUE和Canvas實(shí)現(xiàn)雷霆戰(zhàn)機(jī)打字類小游戲
這篇文章主要介紹了如何用VUE和Canvas實(shí)現(xiàn)雷霆戰(zhàn)機(jī)打字類小游戲,麻雀雖小,五臟俱全,對(duì)游戲感興趣的同學(xué),可以參考下,研究里面的原理和實(shí)現(xiàn)方法2021-04-04
Vue項(xiàng)目如何根據(jù)圖片url獲取file對(duì)象并用axios上傳
這篇文章主要介紹了Vue項(xiàng)目如何根據(jù)圖片url獲取file對(duì)象并用axios上傳問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Vue高級(jí)組件之函數(shù)式組件的使用場(chǎng)景與源碼分析
Vue提供了一種稱為函數(shù)式組件的組件類型,用來定義那些沒有響應(yīng)數(shù)據(jù),也不需要有任何生命周期的場(chǎng)景,它只接受一些props來顯示組件,下面這篇文章主要給大家介紹了關(guān)于Vue高級(jí)組件之函數(shù)式組件的使用場(chǎng)景與源碼分析的相關(guān)資料,需要的朋友可以參考下2021-11-11
Vue3中多個(gè)彈窗同時(shí)出現(xiàn)的解決思路
這篇文章主要介紹了Vue3中多個(gè)彈窗同時(shí)出現(xiàn)的解決思路,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue?+elementui?項(xiàng)目登錄通過不同賬號(hào)切換側(cè)邊欄菜單的顏色
這篇文章主要介紹了vue?+elementui?項(xiàng)目登錄通過不同賬號(hào)切換側(cè)邊欄菜單的顏色,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01

