vue3-defineComponent

802 阅读1分钟

defineComponent函数,只是对setup函数进行封装,返回options的对象;

defineComponent最重要的是:在TypeScript下,给予了组件 正确的参数类型推断 。

import { defineComponent } from "vue";
import s from "./WelcomeLayout.module.scss";

export const WelcomeLayout = defineComponent({
  setup(props, context) {
    const {
      slots: { icon, title, buttons },
    } = context;
    return () => (
      <div class={s.wrapper}>
        <div class={s.card}>
          {icon?.()}
          {title?.()}
        </div>
        <div class={s.actions}>{buttons?.()}</div>
      </div>
    );
  },
});

setup 中的两个参数 props 与 context ,
props指组件传递来的参数,并且ts可以推论出props的类型.props也就是 vue2 中组件中的 props
context 有三个属性 attrs slots emit 分别对应vue2中的 attrs属性、slots插槽、$emit发送事件