# 手写 mini-vue3 - 实现组件对象的代理(三)

实现 this 指向

ts 复制代码
import { h } from '../lib/guide-mini-vue.esm.js';

export const App = {
  name: 'App',
  render() {
    window.self = this;
    return h('div', { id: 'app' }, [
      h('p', { class: 'red' }, 'Hello' + this.msg),
    ]);
  },
  setup() {
    return {
      msg: 'jerry'
    }
  }
}

在这个例子中,我们需在 render 中实现 this 的指向,在 setupRenderEffect 调用 instance.render 时,理论上是让 render 函数指向 setupState。

setupState 指的是 setup 函数的返回结果

ts 复制代码
// 伪代码
instance.render.call(setupState);

实现

在 createComponentInstance 这里,给 instance 初始化了 setupState 属性

ts 复制代码
function createComponentInstance(vnode) {
    const component = {
        setupState: {},
    }
}

在 setupStatefulComponent 这里,实现组件对象的代理

ts 复制代码
// runtime-core/component.ts
function setupStatefulComponent(instance) {
  const Component = instance.type;
  const { setup } = Component;
  

  // 实现组件对象的代理
  instance.proxy = new Proxy({ _: instance }, PublicInstanceProxyHandlers)

  if(setup) {
    const setupResult = setup();
    // 处理 setup 函数的返回结果
    handleSetupResult(instance, setupResult);
  }
}
ts 复制代码
// runtime-core/componentPublicInstance.ts
export const PublicInstanceProxyHandlers = {
  get({ _: instance }, key) {
    const { setupState, props } = instance;
    
    const hasOwn = (val, key) => Object.prototype.hasOwnProperty.call(val, key);

    if(hasOwn(setupState, key)) {
      return setupState[key];
    }
  }
}

回顾 runtime-core 的流程中,等到 setupRenderEffect 执行时,已完成实例对象 proxy 的绑定。

ts 复制代码
function mountComponent() {
    createComponentInstance();
    setupComponent();
    setupRenderEffect();
}

在 setupRenderEffect 内调用 instance.render 时,把实例的代理对象 proxy 绑定到 render,那么 render 函数内的 this 便指向了 instance.proxy

ts 复制代码
// rumtime-core/renderer.ts
function setupRenderEffect(instance) {
    const { proxy } = instance;
    const subTree = instance.render.call(proxy);
    patch(subTree, container);
}

这里需要注意的是,被 Proxy 代理的目标对象是 instance,但是我们在 render 函数内部访问 this.msg 的时候,不是访问 instance 的属性,而是 instance.proxy 的属性。

在访问 this.msg 的时候,被 Proxy 拦截处理,内部的机制是,从 instance.setupState 里面获取 msg 属性值并返回。

ts 复制代码
const { setupState, props } = instance;

if(hasOwn(setupState, key)) { 
    return setupState[key]; 
}

$el

vnode 的 el

ts 复制代码
function createVNode(type, props?, children) {
    const vnode = {
        el: null,
    }
}

开始给 el 赋值是在 mountElement 函数内部处理。

ts 复制代码
function mountElement(vnode, container) {
    const el = vnode.el = document.createElement(vnode.type); // 开始给 el 赋值
}
ts 复制代码
function mountComponent(initialVNode, container) {
    const instance = createComponentInstance(initialVNode)
    setupComponent(instance);
    // 把 initialVNode 传给 setupRenderEffect
    setupRenderEffect(instance, initialVNode, container);
}
ts 复制代码
function setupRenderEffect(instance, initialVNode, container) {
    const subTree = instance.render.call(proxy);
    patch(subTree, container);
    
    // 经过 patch 之后,subTree 获得了 el
    initialVNode.el = subTree.el;
}

为什么是 initialVNode.el = subTree.el; 呢?

vnode 和 subTree 的区别

比如,在这个例子中:

js 复制代码
const Child = {
    name: 'Child',
    render() {
        return h('div', {}, 'child')
    }
};
const App = {
    name: 'App',
    render() {
        return h('div', { class: 'app' }, [
            h(Child, {})
        ])
    }
}

App 的 el 应该是

html 复制代码
<div class="app">
    <div class="child">child</div>
</div>

Child 的 el 应该是:

html 复制代码
<div class="child">child</div>

App vnode 的 el

一开始 App 传给 createVNode 得到了一个 App 的 initialVnode,

ts 复制代码
{
    type: App,
    props: {},
    children,
    el: null,
}

initialVnode 传给了 render

接着 patch(initialVnode)

接着 mountComponent(initialVnode)

接着来到 setupRenderEffect(initialVnode)

接着执行 App instance 的 render,得到 subTree

ts 复制代码
// App vnode 的 subTree
subTree = h('div', { class: 'app' }, [
    h(Child, {})
])

h 函数返回的是:

ts 复制代码
{
    type: 'div',
    props: {
        class: 'app'
    },
    children: h(Child, {}),
    el: null,
}

也就是 subTree 指的是:

ts 复制代码
{
    type: 'div',
    props: {
        class: 'app'
    },
    children: h(Child, {}),
    el: null,
}

接着 patch(subTree)

接着 mountElement(subTree)

接着 subTree.el = document.createElement('div')

至此,patch 完成

接着 initialVnode.el = subTree.el

最后,initialVnode 即 App 对应的这个 vnode 的 el 为 document.createElement('div')

Child vnode 的 el

在第二次 patch 后,根据 type='div'

来到 mountElement

接着判断到 vnode.children 是数组

接着 mountChildren

接着再次 patch

ts 复制代码
mountChildren(vnode) {
    vnode.children.forEach(v => {
        patch(v);
    })
}

这里,v 表示 Child 的 vnode

根据 type=Child

接着来到 mountComponent,这时的 initialVnode 为 Child 的 vnode

ts 复制代码
initialVnode = {
    type: Child,
    props: {},
    children: {},
    el: null,
}

接着来到 setupRenderEffect

接着执行 Child 的 render得到 Child 的 subTree

ts 复制代码
const subTree = instance.render.call(proxy);

即

ts 复制代码
subTree = h('div', { class: 'child' }, 'child')

然后再 patch

最终 Child vnode 的 el 是 <div class="child">child</div>

el 挂载到 instance.proxy 代理对象

ts 复制代码
const PublicPropertiesMaps = {
  $el: (i) => i.vnode.el,
}


export const PublicInstanceProxyHandlers = {
  get({ _: instance }, key) {
    const { setupState, props } = instance;
    
    // 省略...

    // 在这里统一处理 $el、$props 等对象
    const publicGetter = PublicPropertiesMaps[key];
    if(publicGetter) {
      return publicGetter(instance);
    }
  }
}

这样,在 render 中访问 this.$el,就可获取到组件对应的 el。

相关推荐
1-1=03 小时前
Vue 中 Class 与 Style 绑定
前端·javascript·vue.js
闲来没事抠鼻屎3 小时前
Web打印批量出单:vue-print-designer 千页实测
vue.js
用户83134859306983 小时前
Vue3的v-if /v-show/v-else /v-else-if:区别、渲染原理、坑点与性能选型
前端·javascript·vue.js
Bigger3 小时前
别再手搓调色板了:Vue 3 一个组件搞定纯色、渐变和画布吸管取色
前端·vue.js·数据可视化
码云社区6 小时前
Java租房平台源码方案:Vue 3房源审核、UniApp预约看房与房东管理
java·vue.js
徐小夕8 小时前
万字拆解:零向量库、无模型微调,SAG2 凭什么刷新多跳问答 SOTA
前端·vue.js·github
tryCbest1 天前
Vue3—— 第 18 篇:Vue 2 到 Vue 3 迁移指南——升级避坑全记录
前端·vue.js
anyup1 天前
自带 uni-app x 页面级能力,uView Pro X 8 个开箱即用的工具函数
前端·vue.js·uni-app
程序员大阳1 天前
副队长ES6+教程(14)--手写Promise来加深理解
前端·javascript·es6·源码·promise·手写·深层
FungLeo1 天前
成为全栈·Vue 管理后台篇·可折叠侧栏与移动抽屉:后台导航的响应式实现
前端·javascript·vue.js·侧边栏·响应式布局·无障碍·移动端导航