vue3.0 基础用法总结

1.v-model 赋值 
  用法同vue2.0 { { xxx }} 

2.defineComponent
        defineComponent函数,只是对setup函数进行封装,返回options的对象
        defineComponent最重要的是:在TypeScript下,给予了组件 正确的参数类型推断 
         
    2.1 需要注意的地方

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

  • inject、ref 等的类型约束

  • setup 的写法和相应 h 的注入问题

  • tsx 中 v-model 和 scopedSlots 的写法

<script lang="tsx">
import { noop, trim } from 'lodash';
import { inject, Ref, defineComponent, getCurrentInstance, ref } from 'vue';
import filters from '@/filters';

export interface IParentDialog {
  show: boolean;
  specFn: (component_id: HostComponent['id']) => Promise<{ data: DictSpecs }>;
}
 
export default defineComponent<IParentDialog>({
  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);

    return () => (
     <div> code ....  </div>
    );
  }
});
</script>
 
  • 引入 defineComponent() 以正确推断 setup() 组件的参数类型

  • defineComponent 可以正确适配无 props、数组 props 等形式

  • defineComponent 可以接受显式的自定义 props 接口或从属性验证对象中自动推断

  • 在 tsx 中,element-ui 等全局注册的组件依然要用 kebab-case 形式

  • 在 tsx

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值