23-mini-vue 实现 emit 功能

实现 emit 功能

  1. 功能点
  • 实现 emit 函数,用于触发组件自定义事件
  • 需求分析
    • 在组件实例上挂载 emit 方法
    • 组件通过 props 接收事件处理函数
    • 触发事件时调用对应的处理函数
  1. 代码实现
  • 页面上要实现的功能
js 复制代码
// Foo.js 子组件
import { h } from '../lib/guide-mini-vue.esm.js'

export const Foo = {
  setup(props,{emit}) { // ✅ emit 解构传递
    // console.log(props.count, 'count');
    const handleClick = () => {
      emit('add',1,2,3) // ✅ emit 触发父组件自定义事件,并进行参数传递
      emit('add-money-brother') // ✅ emit 触发父组件自定义事件(烤肉串命名)
    }
    return {
      handleClick
    }
  },
  render() {
    const btn = h('button', {
      onClick: this.handleClick
    }, 'emit click')
    return h("div", {
    }, ["foo:" + this.count, btn])
  },
}
js 复制代码
// App.js 父组件
import { h } from '../lib/guide-mini-vue.esm.js'
import { Foo } from './Foo.js'

window.self = null
export const App = {
  name: 'App',
  render() {
    window.self = this
    return h("div",{
      id: 'root',
      class: ['red', 'hard'],
    },
    [h("p",{class:"red"}, "hi"),h(Foo,{count: 1,
      onAdd(a,b,c) { // ✅ 自定义事件触发 + 传参
        console.log('我是父组件',a,b,c);
      },
      onAddMoneyBrother() { // ✅ 烤肉串式命名自定义事件触发
        console.log('I am Father Component');
        
      }
    })]
    )
  },
  setup() {
    return {
      msg: "mini-vue-haha",
    }
  }
}
  • 具体的实现步骤
js 复制代码
// componentEmit.ts  
// ✅ 这里的 emit 方法,挂载在实例上,后续直接传入 setup, 在页面被解构出来使用
//     换句话说,这里的 emit 就是页面上调用的 emit
//     可是页面上 传入的都是 add / add-money-brother 这些名称参数,要和父组件中的 自定义事件名对上,拿到对应的方法执行,就得做一番转换
// TPP 先实现一个特定的需求,然后扩展到通用的需求,这里直接展示的是最终结果
export function emit(instance, event, ...args) {
  // 调用 子组件传过来的函数
  const { props } = instance
  const camelize = (str) => { // ✅ 驼峰转化
    return str.replaceAll(/-(\w)/g, (_, c: string) => {
      return c ? c.toUpperCase() : ''
    })
  }
  const capitalize = (str) => { // ✅ 首字母大写
    return str.charAt(0).toLocaleUpperCase() + str.slice(1)
  }
  const handlerkey = (str) => { // ✅ 拼接出 事件处理函数名
    return str ? 'on' + capitalize(str) : ''
  }
  const handlerName = handlerkey(camelize(event))
  const handler = props[handlerName]
  handler && handler(...args)
}
js 复制代码
// component.ts
import { emit } from "./componentEmit"
export function createComponentInstance(vnode) {
  const component = {
    vnode,
    type: vnode.type,
    setupState: {},
    props: {},
    emit:()=>{} // ✅ 这里初始化占一个位置
  }
  component.emit = emit.bind(null, component) as any // ✅ 这里 null 是this指向, component 作为第一个参数传入
  return component
}
function setupStatefulComponents(instance: any) {
  const Component = instance.vnode.type
  const { setup } = Component
  instance.proxy = new Proxy({_: instance}, PublicInstanceProxyHandlers)
  if(setup) { 
    const setupResult = setup(shallowReadonly(instance.props),{ 
      emit: instance.emit // ✅ 这里将实例上的 emit 传入
    })
    handleSetupResult(instance, setupResult)
  }
}
js 复制代码
// shared/ index.ts ✅ 方法抽离 
export const camelize = (str) => {
  return str.replaceAll(/-(\w)/g, (_, c: string) => {
    return c ? c.toUpperCase() : ''
  })
}
const capitalize = (str) => {
  return str.charAt(0).toLocaleUpperCase() + str.slice(1)
}
export const handlerkey = (str) => {
  return str ? 'on' + capitalize(str) : ''
}
相关推荐
郑州光合科技余经理14 小时前
同城配送调度系统实战:JAVA微服务
java·开发语言·前端·后端·微服务·中间件·php
css趣多多14 小时前
动态路由,路由重置,常量路由,$ref,表单验证流程
开发语言·javascript·ecmascript
一只小bit14 小时前
Qt 绘图核心教程:从基础绘制到图像操作全解析
前端·c++·qt·gui
浪潮IT馆14 小时前
在 VSCode 中调试 JavaScript 的 Jest 测试用例
javascript·ide·vscode
乾元14 小时前
绕过艺术:使用 GANs 对抗 Web 防火墙(WAF)
前端·网络·人工智能·深度学习·安全·架构
HWL567914 小时前
一个CSS属性will-change: transform
前端·css
Y淑滢潇潇14 小时前
WEB 作业 即时内容发布前端交互案例
前端·javascript·交互
比特森林探险记14 小时前
后端开发者快速入门react
开发语言·前端·javascript
李松桃14 小时前
python第三次作业
java·前端·python
一起养小猫15 小时前
Flutter for OpenHarmony 实战:ListView与GridView滚动列表完全指南
开发语言·javascript·flutter