• 如何使用ts深度监听数组或者对象

 @Watch('defaultPage', { deep: true })

  defaultPageChange (newVal: number, oldVal: number) {

    console.log(newVal)

  };

  • 关于$refs的问题,按vue的写法在typescript中会报如下错误
Element implicitly has an 'any' type because type 'Element | Element[] | Vue | Vue[]' has no index signature.

在typescript中用必须要先建立一个变量,在后面在取值,如果是component数组,先建立component数组,在后面在传入数组索引取值。

const el: any = this.$refs.component
const instance: any = el[current]

或者将this 的类型 强行改为any

    (this as any).$refs.component[current];

  • 调用组件传入值时,一直报:Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "data"

如:@Prop() obj: object = {};

原因是:子组件的数据由调用时传入,自己允许修改这个数据,所以就会报这个错,解决方法是:子组件的属性不设初始值,也就是只定义属性名,不赋初始值,上面的栗子改为:

@Prop() obj: object;

  • vue+typescript:this.xxx提示:Property 'xxx' does not exist on type 'Test';错误 

使用(this as any).xxx 进行强制类型转换,不进行类型检查就不会提示报错信息。 (this as any).xxx 的语法表达的含义,更优雅的方式暂时还不知道

百度也有说使用Vue.extend或者vue-class-component的

补充:还有可能出现下面的错误:

private obj: object = {};

使用this.obj.a时报错,可以使用this.obj['a'] 或者将属性改为any 也就是 private obj: any = {}

Logo

前往低代码交流专区

更多推荐