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')
}
}
Unfortunately, I get:
error TS2339: Property '$router' does not exist on type 'Login'.
Shim the vue file and include $router
:
Make a typings
file called vue-shim.d.ts
Adjust your shim like this:
import VueRouter, { Route } from 'vue-router'
declare module 'vue/types/vue' {
interface Vue {
$router: VueRouter
}
}
now Vue
(in your case -- this) will have a property of $router
and typescript won't complain.
Make sure you have the router imported in your main vue file:
import router from './router'
const v = new Vue({
router,
render: h => h(App)
}).$mount('#app')
This is how I write my components, but I think it is the same.
import Vue from 'vue'
export default Vue.extend({
name: 'Login'
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With