欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈Vue3 defineComponent有什么作用

程序员文章站 2022-07-05 22:27:24
目录definecomponent重载函数definecomponent函数,只是对setup函数进行封装,返回options的对象;export function definecomponent(o...

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

export function definecomponent(options: unknown) {
  return isfunction(options) ? { setup: options } : options
}

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

浅谈Vue3 defineComponent有什么作用

definecomponent重载函数

1:direct setup function

// overload 1: direct setup function
// (uses user defined props interface)
export function definecomponent<props, rawbindings = object>(
  setup: (
    props: readonly<props>,
    ctx: setupcontext
  ) => rawbindings | renderfunction
): definecomponent<props, rawbindings>

浅谈Vue3 defineComponent有什么作用

2:object format with no props

// overload 2: object format with no props
// (uses user 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>

浅谈Vue3 defineComponent有什么作用

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,...>

浅谈Vue3 defineComponent有什么作用

4: object format with object props declaration

// overload 4: object format with object props declaration
// see `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>

浅谈Vue3 defineComponent有什么作用

开发实践

除去单元测试中几种基本的用法,在以下的 parentdialog 组件中,主要有这几个实际开发中要注意的点:

自定义组件和全局组件的写法

inject、ref 等的类型约束

setup 的写法和相应 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']) => promise<{ data: dictspecs }>;
}
 
export default definecomponent<iparentdialog>({
  // tsx 中自定义组件依然要注册
  components: {
    childtable
  },
  props: {
    show: {
      type: boolean,
      default: false
    },
    specfn: {
      type: function,
      default: noop
    }
  },
 
  // note: setup 须用箭头函数
  setup: (props, context) => {
 
    // 修正 tsx 中无法自动注入 'h' 函数的问题
    // eslint-disable-next-line no-unused-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(false);
 
    const _lookupspecs = async (component_id: hostcomponent['id']) => {
      loading.value = true;
      try {
        const json = await specfn(component_id);
        dictspecs.value = json.data;
      } finally {
        loading.value = false;
      }
    };
 
    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 false;
      }
      for (let i = 0; i < model.length; i++) {
        const { brand_id, data } = model[i];
        if (!brand_id) {
          err2.value = '请选择品牌';
          return false;
        }
        if (
          formdata.value.model.some(
            (m, midx) => midx !== i && string(m.brand_id) === string(brand_id)
          )
        ) {
          err2.value = '品牌重复';
          return false;
        }
      }
      return true;
    };
 
    const onclose = () => {
      emit('update:show', false);
    };
    const onsubmit = async () => {
      const bool = _docheck();
      if (!bool) return;
      const params = formdata.value;
      emit('submit', params);
      onclose();
    };
 
    // note: 在 tsx 中,element-ui 等全局注册的组件依然要用 kebab-case 形式 ????‍
    return () => (
      <commondialog
        class="comp"
        title="新建"
        width="1000px"
        labelcancel="取消"
        labelsubmit="确定"
        vloading={loading.value}
        show={show}
        onclose={onclose}
        onsubmit={onsubmit}
      >
        <el-form labelwidth="140px" class="create-page">
         <el-form-item label={withcolon('部件类型')} required={true} error={err1.value}>
            <el-select
              class="full-width"
              model={{
                value: formdata.value.component_id,
                callback: (v: string) => {
                  formdata.value.component_id = v;
                  _lookupspecs(v);
                }
              }}
            >
              {dictcomponents.value.map((dictcomp: dictcomp) => (
                <el-option key={dictcomp.id} label={dictcomp.component_name} value={dictcomp.id} />
              ))}
            </el-select>
          </el-form-item>
          {formdata.value.component_id ? (
              <el-form-item labelwidth="0" label="" required={true} error={err2.value}>
                <child-table
                  list={formdata.value.model}
                  onchange={(v: spec['model']) => {
                    formdata.value.model = v;
                  }}
                  onerror={(err: string) => {
                    err3.value = err;
                  }}
                  scopedslots={{
                      default: (scope: any) => (
                        <p>{ scope.foo }</p>
                      )
                  }}
                />
              </el-form-item>
          ) : null}
        </el-form>
      </commondialog>
    );
  }
});
</script>
 
<style lang="scss" scoped>
</style>

全文总结

  • 引入 definecomponent() 以正确推断 setup() 组件的参数类型
  • definecomponent 可以正确适配无 props、数组 props 等形式
  • definecomponent 可以接受显式的自定义 props 接口或从属性验证对象中自动推断
  • 在 tsx 中,element-ui 等全局注册的组件依然要用 kebab-case 形式
  • 在 tsx 中,v-model 要用 model={{ value, callback }} 写法
  • 在 tsx 中,scoped slots 要用 scopedslots={{ foo: (scope) => () }} 写法
  • definecomponent 并不适用于函数式组件,应使用 rendercontext 解决

到此这篇关于浅谈vue3 definecomponent有什么作用的文章就介绍到这了,更多相关vue3 definecomponent作用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!