欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+ts中ref與reactive指定類型實現(xiàn)示例

 更新時間:2022年06月27日 15:41:26   作者:wyc0859  
這篇文章主要為大家介紹了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)文章

最新評論