vuejs typescript 属性路由器不存在 [英] vuejs typescript property router does not exist

查看:73
本文介绍了vuejs typescript 属性路由器不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的打字稿类组件中访问路由器:

I try to access router in my typescript class component:

import {Vue} from 'vue-property-decorator'
import Component from 'nuxt-class-component'
import {Getter, Action} from 'vuex-class'

@Component
export default class Login extends Vue {
    @Action login

    username = ''
    password = ''

    async submit () {
        await this.login({username: this.username, password: this.password})
        this.$router.push('/results')
    }
}

不幸的是,我明白了:

error TS2339: Property '$router' does not exist on type 'Login'.

推荐答案

Shim the vue file and include $router:

Shim the vue file and include $router:

制作一个名为 vue-shim.d.ts

像这样调整垫片:

import VueRouter, { Route } from 'vue-router'

declare module 'vue/types/vue' {
  interface Vue {
    $router: VueRouter
  }
}

现在 Vue(在你的情况下——这个)将有一个 $router 的属性并且打字稿不会抱怨.

now Vue (in your case -- this) will have a property of $router and typescript won't complain.

这篇关于vuejs typescript 属性路由器不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆