我尝试在我的打字稿类组件中访问路由器:
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'.
原文由 Chris 发布,翻译遵循 CC BY-SA 4.0 许可协议
填充 vue 文件并包含
$router
:制作一个名为
typings
的文件vue-shim.d.ts
像这样调整你的垫片:
现在
Vue
(在你的情况下 - 这个)将具有$router
的属性并且打字稿不会抱怨。