Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vuejs typescript property router does not exist

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'.
like image 877
Chris Avatar asked Sep 08 '17 09:09

Chris


2 Answers

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.

like image 129
Ohgodwhy Avatar answered Sep 20 '22 08:09

Ohgodwhy


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'

  })
like image 43
user2062241 Avatar answered Sep 23 '22 08:09

user2062241