vue + typescript:Property 'xxx' does not exist on type 'Vue'.

问题:vue + typescript实践。vscode 提示vue对象下没有定义的属性和方法(其实是已经定义过了)

相关代码和提示信息:
export default Vue.extend({
  props: {
    menu: {
      type: Array,
      default: () => []
    }
  },
  name: 'navHeaderMenu',
  data() {
    return {
      hoverIndex: ''
    }
  },
  methods: {
    triggerToMousemove(i: number): void {
      // 如果是下面这种语法:
      // vscode 会提示:Property 'hoverIndex' does not exist on type 'Vue'.
      this.houverIndex = i
      // vscode 的提示:Property 'test' does not exist no type 'Vue'.
      this.ab()
      // 如果是下面这种语法:
      // vscode 并不会报错
      (this as any).houverIndex = i
      (this as any).test()
      this.$emit('mouseover', i)
    },
    triggerToMouseout(i: number): void {
      (this as any).hoverIndex = ''
      this.$emit('mouseout', i)
    },
    test() {
        console.log('test')
    },
  },
})

疑问

  • vscode 为什么会提示: Property xxx does not exist on type 'Vue'
  • 百度了之后,用 typescript 的这个语法(this as any).xxx 就不会提示报错信息。不明白 (this as any).xxx 的语法表达的含义,大佬们可以指点下吗,谢谢.
  • 除了(this as any).xxx 语法,还有没有其他方式可以解决类似问题的报错
阅读 21.1k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题