Vue3-defineComponent解决了什么?

更新时间:2023-07-10 05:53:40 阅读: 评论:0

Vue3-defineComponent解决了什么?
defineComponent函数,只是对tup函数进⾏封装,返回options的对象;
export function defineComponent(options: unknown) {
return isFunction(options) ? { tup: options } : options
}
defineComponent最重要的是:在TypeScript下,给予了组件 正确的参数类型推断 。
index.tsx
defineComponent重载函数
1:direct tup function
// overload 1: direct tup function
// (us ur defined props interface)
export function defineComponent<Props, RawBindings = object>(
tup: (
props: Readonly<Props>,
ctx: SetupContext
) => RawBindings | RenderFunction
): DefineComponent<Props, RawBindings>
2:object format with no props
// overload 2: object format with no props
// (us ur defined props interface)
// return type is for Vetur and TSX support
export function defineComponent<
Props = {},
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
润肺止咳的食物
EE extends string = string
>(
options: ComponentOptionsWithoutProps<Props,RawBindings,D,C,M,Mixin,Extends,E,EE> ): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
3:object format with array props declaration
// overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: any }
// return type is for Vetur and TSX support
export function defineComponent<
PropNames extends string,
RawBindings,
D,
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
>(
options: ComponentOptionsWithArrayProps<
PropNames,
RawBindings,...>
): DefineComponent<
Readonly<{ [key in PropNames]?: any }>,
RawBindings,...>
4: object format with object props declaration
// overload 4: object format with object props declaration
// e `ExtractPropTypes` in ./componentProps.ts
export function defineComponent<
// the Readonly constraint allows TS to treat the type of { required: true }
// as constant instead of boolean.
PropsOptions extends Readonly<ComponentPropsOptions>,
RawBindings,
D,
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
>(
options: ComponentOptionsWithObjectProps<
PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
开发实践
除去单元测试中⼏种基本的⽤法,在以下的 ParentDialog 组件中,主要有这⼏个实际开发中要注意的点:⾃定义组件和全局组件的写法
inject、ref 等的类型约束
tup 的写法和相应 h 的注⼊问题
tsx 中 v-model 和 scopedSlots 的写法
ParentDialog.vue
<script lang="tsx">
import { noop, trim } from 'lodash';
import {
inject, Ref, defineComponent, getCurrentInstance, ref
} from '@vue/composition-api';
竹笆脚手板import filters from '@/filters';
import CommonDialog from '@/components/CommonDialog';人的生殖和发育
import ChildTable, { getEmptyModelRow } from './ChildTable.vue';
export interface IParentDialog {
落实会议精神show: boolean;
specFn: (component_id: HostComponent['id']) => Promi<{ data: DictSpecs }>;
}
export default defineComponent<IParentDialog>({
// tsx 中⾃定义组件依然要注册
components: {
ChildTable
},
props: {
show: {
type: Boolean,
default: fal
},
specFn: {
type: Function,
default: noop
}
},
// note: tup 须⽤箭头函数
tup: (props, context) => {
// 修正 tsx 中⽆法⾃动注⼊ 'h' 函数的问题
// eslint-disable-next-line no-unud-vars
const h = getCurrentInstance()!.$createElement;
const { emit } = context;
const { specFn, show } = props;
// filter 的⽤法
const { withColon } = filters;
// inject 的⽤法
const pageType = inject<CompSpecType>('pageType', 'foo');
const dictComponents = inject<Ref<DictComp[]>>('dictComponents', ref([]));
// ref的类型约束
const dictSpecs = ref<DictSpecs>([]);
const loading = ref(fal);
const _lookupSpecs = async (component_id: HostComponent['id']) => {
loading.value = true;
try {
const json = await specFn(component_id);
dictSpecs.value = json.data;
} finally {
loading.value = fal;
}
};
const formdata = ref<Spec>({
component_id: '',
specs_id: '',
model: [getEmptyModelRow()]
});
const err1 = ref('');
const err2 = ref('');
const _doCheck = () => {
err1.value = '';
过期橄榄油
err2.value = '';
const { component_id, specs_id, model } = formdata.value;
if (!component_id) {
err1.value = '请选择部件';
return fal;
}
挚爱是什么意思
for (let i = 0; i < model.length; i++) {
for (let i = 0; i < model.length; i++) {情侣名古风
const { brand_id, data } = model[i];
if (!brand_id) {
err2.value = '请选择品牌';
return fal;
}
if (
del.some(
(m, midx) => midx !== i && String(m.brand_id) === String(brand_id)
)
) {
err2.value = '品牌重复';
return fal;
}
}
return true;
};
const onClo = () => {
emit('update:show', fal);
};
const onSubmit = async () => {
const bool = _doCheck();
if (!bool) return;
const params = formdata.value;
emit('submit', params);
onClo();
};
// note: 在 tsx 中,element-ui 等全局注册的组件依然要⽤ kebab-ca 形式
return () => (
<CommonDialog
申报税务流程class="comp"
title="新建"
width="1000px"
labelCancel="取消"
labelSubmit="确定"
vLoading={loading.value}
show={show}
onClo={onClo}
onSubmit={onSubmit}
>
<el-form labelWidth="140px" class="create-page">
<el-form-item label={withColon('部件类型')} required={true} error={err1.value}>
<el-lect
class="full-width"
model={{
value: ponent_id,
callback: (v: string) => {
ponent_id = v;
_lookupSpecs(v);
}
}}
>
{dictComponents.value.map((dictComp: DictComp) => (
<el-option key={dictComp.id} label={ponent_name} value={dictComp.id} />              ))}
</el-lect>
</el-form-item>
{ponent_id ? (
<el-form-item labelWidth="0" label="" required={true} error={err2.value}>
<child-table
list={del}
onChange={(v: Spec['model']) => {
del = v;
}}

本文发布于:2023-07-10 05:53:40,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1075326.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:组件   函数   类型   开发   全局   返回
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图