从零学习做网站,江都区城乡建设局网站马局,汉服网站设计目的,企业邮箱怎么使用前言
上一篇文章介绍了vue/composition-api是什么#xff0c;以及为什么要用#xff0c;现在来系统地解析一下 vue/composition-api 的实现原理#xff0c;希望可以加深对其工作机制的理解。
老规矩先分享下AI评价#xff1a;对vue/composition-api实现原理的介绍整体上非…前言
上一篇文章介绍了vue/composition-api是什么以及为什么要用现在来系统地解析一下 vue/composition-api 的实现原理希望可以加深对其工作机制的理解。
老规矩先分享下AI评价对vue/composition-api实现原理的介绍整体上非常详细和准确展示了核心代码以及关键逻辑这为理解其工作机制提供了很好的分析。
核心代码分析
install
引入vue/composition-api时需要手动通过Vue.use()来调用该插件的install所以我们看看安装时到底做了什么 export function install(Vue: VueConstructor) {setVueConstructor(Vue)mixin(Vue)
}这里最重要的其实是mixin函数定义了在beforeCreate钩子中去执行初始化 构建props和上下文ctx对象执行setup函数
export function mixin(Vue: VueConstructor) {Vue.mixin({beforeCreate: functionApiInit,mounted(this: ComponentInstance) {afterRender(this)},beforeUpdate() {updateVmAttrs(this as ComponentInstance)},updated(this: ComponentInstance) {afterRender(this)},})function functionApiInit(this: ComponentInstance) {const vm thisconst $options vm.$optionsconst { setup, render } $optionsconst { data } $options// wrapper the data option, so we can invoke setup before data get resolved$options.data function wrappedData() {// 在data周期时进行setup函数的执行initSetup(vm, vm.$props)return data || {}}}function initSetup(vm: ComponentInstance, props: Recordany, any {}) {const setup vm.$options.setup!// 构造ctx对象有以下key// slots: 组件的插槽,默认为 {}// root: 组件的根实例// parent: 组件的父实例// refs: 组件的 ref 引用// listeners: 组件的事件监听器// isServer: 是否是服务端渲染// ssrContext: 服务端渲染的上下文// emit: 组件的自定义事件触发函数const ctx createSetupContext(vm)const instance toVue3ComponentInstance(vm)instance.setupContext ctx// 通过Vue.observable对props进行响应式监听def(props, __ob__, createObserver())}
}ref
ref函数可以把一个普通的值转成响应式的数据。它返回一个可变的ref对象,对象上挂载了一个.value属性,我们可以通过这个.value属性读取或者修改ref的值。
其本质还是基于reactive来实现的响应式只不过做了一个前置的get、set封装源码如下
export function ref(raw?: unknown) {// ref的本质其实还是调用的reactive// 然后通过 get/set 方法操作这个对象的值来实现对 ref 值的跟踪和响应const value reactive({ [RefKey]: raw })return createRef({get: () value[RefKey] as any,set: (v) ((value[RefKey] as any) v),})
}createRef最后还是基于Object.defineProperty
export function proxy(target: any,key: string,{ get, set }: { get?: Function; set?: Function }
) {Object.defineProperty(target, key, {enumerable: true,configurable: true,get: get || noopFn,set: set || noopFn,})
}reactive
底层基于Vue.observable来建立响应式监听Vue.observable 在 Vue2 和 Vue3 中的实现方式有所区别而已
export function reactiveT extends object(obj: T): UnwrapRefT {// 基于Vue.observableconst observed observe(obj)setupAccessControl(observed)return observed as UnwrapRefT
}computed
computed函数用来创建一个计算属性它根据依赖进行缓存和懒执行。
构建get、set函数通过vue的Watcher对象进行监听绑定。
export function computedT(getterOrOptions: ComputedGetterT | WritableComputedOptionsT
): ComputedRefT | WritableComputedRefT {const vm getCurrentScopeVM()let getter: ComputedGetterTlet setter: ComputedSetterT | undefinedgetter getterOrOptions.getsetter getterOrOptions.setlet computedSetterlet computedGetterconst { Watcher, Dep } getVueInternalClasses()let watcher: anycomputedGetter () {if (!watcher) {watcher new Watcher(vm, getter, noopFn, { lazy: true })}if (watcher.dirty) {watcher.evaluate()}if (Dep.target) {watcher.depend()}return watcher.value}computedSetter (v: T) {if (setter) {setter(v)}}return createRefT({get: computedGetter,set: computedSetter,},!setter,true) as WritableComputedRefT | ComputedRefT
}watch
watch函数用于侦听特定的数据源,并在回调函数中执行副作用。
底层是通过Vue.$watch来实现的
// 底层是通过Vue.$watch来实现
function createVueWatcher(vm: ComponentInstance,getter: () any,callback: (n: any, o: any) any,options: {deep: booleansync: booleanimmediateInvokeCallback?: booleannoRun?: booleanbefore?: () void}
): VueWatcher {const index vm._watchers.length// ts-ignore: use undocumented optionsvm.$watch(getter, callback, {immediate: options.immediateInvokeCallback,deep: options.deep,lazy: options.noRun,sync: options.sync,before: options.before,})return vm._watchers[index]
}toRefs
toRefs 可以把一个 reactive 对象的属性都转成 ref 形式。
底层是一个个遍历key去调用toRef然后基于createRef来保留响应式能力
export function toRefT extends object, K extends keyof T(object: T,key: K
): RefT[K] {if (!(key in object)) set(object, key, undefined)const v object[key]if (isRefT[K](v)) return vreturn createRef({get: () object[key],set: (v) (object[key] v),})
}生命周期
生命周期其实是一个临时覆盖
// 利用 Vue 的选项合并策略(option merge strategies),
// 通过给组件实例的 $options 注入自定义的生命周期hook函数,来override原有的生命周期选项。// 生命周期hook函数需要通过 getCurrentInstance() 获取当前活跃的组件实例,
// 并在执行回调前通过 setCurrentInstance()绑定实例,在回调执行完毕后恢复之前的实例。export const onBeforeMount createLifeCycle(beforeMount)
export const onMounted createLifeCycle(mounted)
export const onBeforeUpdate createLifeCycle(beforeUpdate)
export const onUpdated createLifeCycle(updated)
export const onBeforeUnmount createLifeCycle(beforeDestroy)
export const onUnmounted createLifeCycle(destroyed)
export const onErrorCaptured createLifeCycle(errorCaptured)
export const onActivated createLifeCycle(activated)
export const onDeactivated createLifeCycle(deactivated)
export const onServerPrefetch createLifeCycle(serverPrefetch)
文章转载自: http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.mgwpy.cn.gov.cn.mgwpy.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.kltsn.cn.gov.cn.kltsn.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.wlqbr.cn.gov.cn.wlqbr.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.rqwwm.cn.gov.cn.rqwwm.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.qttg.cn.gov.cn.qttg.cn http://www.morning.ltywr.cn.gov.cn.ltywr.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn