1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| function mountComponent(vnode,container,anchor) { const componentOptions = vnode.type let {render,data,setup} = componentOptions beforeCreate && beforeCreate() const state = data ? reactive(data()) : null const [props,attrs] = resolveProps(propsOption,vnode,props) const instance = { state, props:shallowReactive(props), isMounted: false, subTree: null } const setupContext = {attrs} const setupResult = setup(shallowReadOnly(instance.props),setupContext) if(typeof setupResult === 'function'){ if(render)console.error('setup函数返回渲染函数,render选项被忽略') render = setupResult }else{ setupState = setupContext } const renderContext = new Proxy(instance,{ get(t,k,t){ const {state,props,slots} = t if(state && k in state) { return state[k] }else if(k in props){ return props[k] }else { console.error('不存在') } if(k === '$slots')return slots }, set(t,k,v,r){ const [state,props] = t if(state && k in state){ state[k]=t }else if(k in props){ props[k]=t }else{ console.error('不存在') } } })
}
function resolveProps(options,propsData) { const props = {} const attrs = {} for(const key in propsData) { if(key in options){ props[key] = propsData[key] }else{ attrs[key] = propsData[key] } } return [props,attrs] }
|