필기 vue3 관찰자 모드 미니 버전

2329 단어
다음 코드를 파일로 저장하면 효과를 볼 수 있습니다! const weakMap = new WeakMap const effectStack = [] let activeEffect function pushStack(o) { effectStack.push(o); activeEffect = o } function popStack() { effectStack.pop(); activeEffect = effectStack[effectStack.length - 1] } function trigger(obj, k) { let k2depMap = weakMap.get(obj) if (!k2depMap) return const dep = k2depMap[k] if (!dep) return dep.forEach(e => e()) } function track(obj, k) { if (!activeEffect) return let k2depMap = weakMap.get(obj) if (!k2depMap) weakMap.set(obj, k2depMap = {}) let dep = k2depMap[k] if (!dep) dep = k2depMap[k] = new Set dep.add(activeEffect) } function effect(fn, option = {}) { const effect = function() { try { pushStack(fn) return fn() } finally { popStack() } } if (!option.lazy) effect() return effect } function reactive(state) { return new Proxy(state, { set(target, k, v) { target[k] = v trigger(target, k, v) }, get(target, k) { track(target, k) return target[k] } }) }
const state = reactive({ count: 1, circle: true }) effect(() => { const div = document.getElementById('btn') div.innerText = ' _ ' + state.count }) effect(() => { const div = document.getElementById('circle') const style = { borderRadius: state.circle ? '20px' : 0 } Object.assign(div.style, style) }) function btn_click() { state.count++ } function circle_click() { state.circle = !state.circle }

좋은 웹페이지 즐겨찾기