Nuxt 自定义路由

步骤:

  1. 在根目录中创建一个 router (目录名字自定义),其下创建一个 index.js(其名字自定义)。如下:


  2. 在 router -> index.js 中添加如下代码:
    // 引入path
    import path from 'path'
    // 要使用获取当前路径 process.cwd()
    const resolve = (pagePath: string) => path.resolve(process.cwd(), `./${pagePath}`)
    const routers = [
        {
            path: '/',
            name: 'index',
            component: resolve('pages/index.vue'),
            meta: {
                title: '首页',
                icon: 'icon-home'
            }
        }
    ]
    
    const router = {
        base: '/',
        extendRoutes: (routes: any) => {
            routes.length = 0
            routes.push(...routers)
        }
    }
    
    export default router

    extendRoutes 说明可以查看 https://www.nuxtjs.cn/api/configuration-router


  3. 在 nuxt.config.js 中引入 路由。

     

     运行 yarn dev 或者 npm run dev 

posted @ 2022-07-01 14:23  闹一腾  阅读(1120)  评论(0)    收藏  举报