简体   繁体   中英

Vue3 Router with Typescript: missing RouteConfig

I created a new vue project from @vue/cli $> vue create my-project , activated the Typescript option and router option, and upgraded to vue3 beta with $>vue add vue-next .

Now, $>npm run serve fails with

ERROR in /home/auser/dev/my-project/src/router/index.ts(1,10):
1:10 Module '"../../node_modules/vue-router/dist/vue-router"' has no exported member 'RouteConfig'.

  > 1 | import { RouteConfig, createRouter, createWebHistory } from 'vue-router';
      |          ^
    2 | import Home from '../views/Home.vue'

The entire file is not that long, and RouteConfig is used later on:

//index.ts
import { RouteConfig, createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue'

const routes: Array<RouteConfig> = [
{
  path: '/',
  name: 'Home',
  component: Home
},
{
  path: '/about',
  name: 'About',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

Q : What is the correct type of RouteConfig, which I need for createRouter ?

you can use like this.

import type { RouteRecordRaw } from 'vue-router';

const router = createRouter({
  history: createWebHashHistory(),
  routes: basicRoutes as RouteRecordRaw[]
});

(Since this is pre-release: I am using vue-router@4.0.0-alpha.13.)

I ran into the same thing. The correct type for routes array is RouteRecordRaw according to the view-router.d.ts; RouteConfig is for the createRouter argument itself.

But if I typed the array as RouteRecordRaw then i ran into the problem of RouteRecordRaw definition is composed of several others types that look like they are not exported, so typing the array causes more problems than it solves.

I suspect that a better pattern will emerge, but for the moment I typed the routes array as any , which works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM