1

有什么办法可以导入,这个路由变量在另一个文件中。注意我不想导入路由变量。下面的代码是我试图做的

src/router.js

const routes = [
{
    path:"/",
    name:"home",
    component: HomeTemplate
},
{
    path:"/heroes",
    name:"heroes",
    component: /*webpackChunkName: Heroes*/ import("@/components/DcHeroes/DcHeroes")
},
{
    path:"/calendar",
    name:"calendar",
    component: /*webpackChunkName: Calendar*/ import("@/components/CalendarTemplate")
},
{
    path:"/markdown",
    name:"markdown",
    component: /*webpackChunkName: Markdown*/ import("@/components/MarkDownTemplate")
},
]
const router = createRouter({
    history:createWebHistory(),
    routes,
    base:'/'
})
export default router;

src/components/navbar/index.vue

<template>
    <div>
        <h2>Navbar</h2>
        <router-link :to="{ name:'home', }">Home</router-link>
        <router-link :to="{ name:'heroes', }">Heroes</router-link>
        <router-link :to="{ name:'calendar', }">Calendar</router-link>
        <router-link :to="{ name:'markdown', }">MarkDown</router-link>
    </div>
</template>
<script>
        import routes from '@/Router.js'
        export default{
            name: "NavbarTemplate",
            mounted(){
                console.log(routes);
            }
        }
</script>

//import {routes} from '@/Router.js'
// This returns a large object

谢谢。

4

1 回答 1

3

你可以使用export const routes喜欢

export const routes = [
{
    path:"/",
    name:"home",
    component: HomeTemplate
},
...
]

然后像这样导入它:

 import { routes } from '@/Router.js'

或者您可以使用this.$router.getRoutes()以下方法访问该路由:

<script>
    
        export default{
            name: "NavbarTemplate",
            mounted(){
                console.log(this.$router.getRoutes());
            }
        }
</script>
于 2021-01-20T17:41:14.180 回答