美文网首页
Vue3.0 踩坑实录

Vue3.0 踩坑实录

作者: 这名字真不对 | 来源:发表于2020-10-05 13:47 被阅读0次

原文发于: https://www.yuque.com/xiongzichao/blog/gvk7gt

Vue3.0 one piece版本已于近日发布,作为Vue铁粉,来尝尝鲜,体验一下正式版本的各种特性。这里记录一个小组件从Vue2.x -> Vue3.0的踩坑过程,以及官方未提到的点。

vue-easy-lightbox 是Vue2.x+TypeScript编写的组件库,.vue单文件使用的是vue-property-decorator ,由于Vue3.0原生支持了ts,直接改写为官方推荐写法(TypeScript 支持 - 定义 Vue 组件)。

Props类型声明

Vue 对定义了 type 的 prop 执行运行时验证,我们通常需要为props声明运行时构造函数类型:

import { defineComponent, PropType } from 'vue'

interface ComplexItem {
  title: string
  id: number
}

// 建议业务中注明 default / required
const Component = defineComponent({
  props: {
    list: {
        type: Array,
      default: () => [],
      required: true
    },
    mixProps: {
      type: [Array, String]
    },
    item: {
      // 如果一个prop需要具体类型,通过官方的泛型接口PropType强制转换类型
      type: Object as PropType<ComplexItem>
    },
    mixListAndString: {
      // 如果type接受的是数组,需要将整个数组强制转换类型
      // 注意,不是将单个数组成员类型转换
      type: [Array, String] as PropType<ComplexItem[] | string>
    }
  },
  methods: {
     testProps() {
       this.list // (property) list: unknown[]
       this.mixProps // (property) mixProp?: string | unknown[] | undefined
       this.item // (property) item?: ComplexItem | undefined
       this.mixListAndString // (property) mixListAndString?: string | ComplexItem[] | undefined
     }
  }
})

Vue 单文件中,Transition 组件的子元素检查

Vue3.0 中, @vue/compile-sfc 模板编译和dev运行时检查都添加了 transtion子元素检查:

  1. 模板编译中, transition 子元素不允许多个组件或元素,否则编译不通过,根据warnTransitionChildren.spec.ts 测试文件,如果需要多个分支,可以使用 v-if + v-if-else 来确定具体分支。
  2. 运行时检查中,需要特别注意 transition 子元素内不能包含注释(这个坑踩了整整2天),否则无法通过运行时编译,导致组件不能正确渲染。

相关文章

  • Vue3.0 踩坑实录

    原文发于: https://www.yuque.com/xiongzichao/blog/gvk7gt[https...

  • vue3.0数组清空与重新赋值

    记个踩坑笔记 vue3.0里面,如果数组是用reactive()声明的,要清空数组得用list.length = ...

  • Vue踩坑实录(二)

    在上一篇中说了一下踩过的前三个坑,剩下的坑就在这篇中全部搞定吧。Vue踩坑实录(一) Vue-cli .js?.V...

  • fastlane 踩坑实录

    这个世界是懒人创造的。 人懒了就会发明各种各样的工具,或者寻找各种各样能够给自己偷懒机会的工具,当然我还停留在使用...

  • Mongo踩坑实录

    1.更新数据时,js脚本中没有指定数据类型,int数据被更新成了double,导致线上问题。 原因,js是弱类型,...

  • gitattribute踩坑实录

    工欲善其事,必先利其器。 前一阵子,公司的版本控制从svn迁移到了git,不得不说,git确实比svn要强大好多,...

  • Weex踩坑实录

    1.新组件无法与配合使用。目前遇到的情况主要是loading没法正常...

  • # Flutter 踩坑实录

    [TOC] Another exception was thrown: A dismissed Dismissib...

  • [php]踩坑实录

    1、strpos()函数的返回值false与0问题 strpos函数定义:int strpos ( string ...

  • 腾讯地图基于 WebGL实现自定义栅格图层踩坑实录

    以下内容转载自totoro的文章《WebGL-Y轴翻转踩坑实录》作者:totoro链接:https://blog....

网友评论

      本文标题:Vue3.0 踩坑实录

      本文链接:https://www.haomeiwen.com/subject/tbrauktx.html