首页 > 其他 > 详细

vue3中手写shallowReactive, shallowRef(系列十三)

时间:2021-03-05 22:22:19      阅读:170      评论:0      收藏:0      [点我收藏+]
function shallowRef(val) {
    return shallowReactive({value:val});
}

function shallowReactive(obj) {
    return new Proxy(obj, {
        get(obj, key){
            return obj[key];
        },
        set(obj, key, val){
            obj[key] = val;
            console.log(更新UI界面);
            return true;
        }
    })
}
let obj = {
    a:a,
    gf:{
        b:b,
        f:{
            c:c,
            s:{
                d:d
            }
        }
    }
};
/*
let state = shallowReactive(obj);

//只有最外层的a属性变化,才会触发视图更新
// state.a = ‘1‘;
state.gf.b = ‘2‘;
state.gf.f.c = ‘3‘;
state.gf.f.s.d = ‘4‘;
 */
let state = shallowRef(obj);

// state.value.a = ‘1‘;
// state.value.gf.b = ‘2‘;
// state.value.gf.f.c = ‘3‘;
// state.value.gf.f.s.d = ‘4‘;
// ref的最外层属性value,只有它改变,视图才会跟新
state.value = {
    a:1,
    gf:{
        b:2,
        f:{
            c:3,
            s:{
                d:4
            }
        }
    }
}

 

vue3中手写shallowReactive, shallowRef(系列十三)

原文:https://www.cnblogs.com/fsg6/p/14487304.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!