import {ref,reactive} from 'vue';
setup() {
const num = ref(123);
num.value = 456;
const obj = reactive({num:123});
obj.value = 456;
}
import {isRef,isReactive} from 'vue';
setup() {
const num = ref(123)
console.log(isRef(num)) // => true
}
toRef (一个属性)
import { toRef , reactive } from 'vue';
setup() {
const obj = reactive({ width:10, height:20 })
const width = toRef(obj,'width')
return {
width
}
}
toRefs ( 所有 )
import { toRefs , reactive } from 'vue';
setup() {
const obj = reactive({ width:10, height:20 })
const { width, height }= toRefs(obj)
return {
width, height
}
}
import { toRefs , reactive } from 'vue';
setup() {
const obj = reactive({ width:10, height:20 })
return {
...toRefs(obj)
}
}
toRaw ( 不影响本身 )
import {toRaw} from 'vue';
setup(){
const num1 = ref(123)
const num2 = toRaw(num1)
console.log(num2 === 123) //=>true
}
markRaw ( 添加 非响应对象 属性 )
import { markRaw, reactive } from "vue";
setup(){
const obj = reactive({ num:1 }); //让数据无法被追踪
obj.noUpdate = markRaw({num:1});
function add() {
obj.num++; // 页面数据 会更新
obj.noUpdate.num++; //页面数据 不会更新
}
return { obj , add }
}
const aaa = ref('abc');
const bbb = unref(aaa);
shallowRef 、shallowReactive(非递归监听)
import {shallowRef,shallowReactive} from 'vue';
setup(){
const obj1 = shallowRef({a:1,b:{c:2})
const obj2 = shallowReactive({a:1,b:{c:2})
obj1.value.a = 2 //页面未更新
obj2.b.c = 3 //页面未更新
}
import {triggerRef, shallowRef} from 'vue';
setup(){
const obj1 = shallowRef({a:1,b:{c:2})
obj1.value.a = 2 //页面没有发生更新,因为只监听value第一层
triggerRef(obj1); // 强制更新
华为社招直通车 华为od面试流程
总结不易,希望uu们不要吝啬你们的哟(^U^)ノ~YO!!如有问题,欢迎评论区批评指正