账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue + typescript:Property 'xxx' does not exist on type 'Vue'.
    59
    0

    问题: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 语法,还有没有其他方式可以解决类似问题的报错
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部