vuejs typescript property router does not exist
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' })
For the ones who are using Vue 3, don't forget to declare your components with defineComponent
, otherwise TypeScript won't be able to infer the types:
<script lang="ts">import { defineComponent } from 'vue'export default defineComponent({ // type inference enabled})</script>
Reference: https://v3.vuejs.org/guide/typescript-support.html#defining-vue-components