组件 Emits
开发者接口
继 props 之后,让我们实现 emits.
emits 的实现相对简单,所以会很快完成.
在开发者接口方面,emits 将从 setup 函数的第二个参数接收.
ts
const MyComponent: Component = {
props: { someMessage: { type: String } },
setup(props: any, { emit }: any) {
return () =>
h('div', {}, [
h('p', {}, [`someMessage: ${props.someMessage}`]),
h('button', { onClick: () => emit('click:change-message') }, [
'change message',
]),
])
},
}
const app = createApp({
setup() {
const state = reactive({ message: 'hello' })
const changeMessage = () => {
state.message += '!'
}
return () =>
h('div', { id: 'my-app' }, [
h(
MyComponent,
{
'some-message': state.message,
'onClick:change-message': changeMessage,
},
[],
),
])
},
})
实现
与 props 类似,让我们创建一个名为 ~/packages/runtime-core/componentEmits.ts
的文件并在那里实现它.~/packages/runtime-core/componentEmits.ts
ts
export function emit(
instance: ComponentInternalInstance,
event: string,
...rawArgs: any[]
) {
const props = instance.vnode.props || {}
let args = rawArgs
let handler =
props[toHandlerKey(event)] || props[toHandlerKey(camelize(event))]
if (handler) handler(...args)
}
~/packages/shared/general.ts
ts
export const capitalize = (str: string) =>
str.charAt(0).toUpperCase() + str.slice(1)
export const toHandlerKey = (str: string) => (str ? `on${capitalize(str)}` : ``)
~/packages/runtime-core/component.ts
ts
export interface ComponentInternalInstance {
// .
// .
// .
emit: (event: string, ...args: any[]) => void
}
export function createComponentInstance(
vnode: VNode,
): ComponentInternalInstance {
const type = vnode.type as Component
const instance: ComponentInternalInstance = {
// .
// .
// .
emit: null!, // to be set immediately
}
instance.emit = emit.bind(null, instance)
return instance
}
您可以将此传递给 setup 函数.
~/packages/runtime-core/componentOptions.ts
ts
export type ComponentOptions = {
props?: Record<string, any>
setup?: (
props: Record<string, any>,
ctx: { emit: (event: string, ...args: any[]) => void },
) => Function // 接收 ctx.emit
render?: Function
}
ts
const mountComponent = (initialVNode: VNode, container: RendererElement) => {
const instance: ComponentInternalInstance = (initialVNode.component =
createComponentInstance(initialVNode));
const { props } = instance.vnode;
initProps(instance, props);
const component = initialVNode.type as Component;
if (component.setup) {
// 传递 emit
instance.render = component.setup(instance.props, {
emit: instance.emit,
}) as InternalRenderFunction;
}
让我们用我们之前假设的开发者接口示例来测试功能!
如果它正常工作,您现在可以使用 props/emit 在组件之间进行通信!
到此为止的源代码:
chibivue (GitHub)