Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议添加vite-plugin-pages 自动化载入路由,还有侧边栏自动生成 #36

Open
cgisky1980 opened this issue Oct 11, 2023 · 6 comments

Comments

@cgisky1980
Copy link

建议添加vite-plugin-pages 自动化载入路由,自动加载 views 下面 子目录的 pages中的 *.vue

还有侧边栏 是否可以在 views 目录下的 子目录 结构,自动生成侧边栏

i18n 文件 在子目录设置 i18n目录,写在一起,维护比较麻烦

@cgisky1980
Copy link
Author

cgisky1980 commented Oct 11, 2023

我自己改写 apps.routes.ts 实现了一下路由自动加载,不过 没有 vite-plugin-pages 强大,没实现 /:id 这样的路由,vpp 插件可以实现。

const metaRouters = import.meta.glob("@/views/demos/**/App.vue", {
  eager: true,
});

export const routes: Array<{
  path: string;
  meta: {
    requiresAuth: boolean;
    layout: string;
    category: string;
  };
  component: () => Promise<any>;
  children: Array<{
    path: string;
    redirect?: string;
    name?: string;
    component?: () => Promise<any>;
  }>;
}> = [];

Object.keys(metaRouters).forEach((item) => {
  let dirname = item.match(/\/src\/views\/demos\/(\S*)\/App.vue/);

  if (dirname !== null && dirname[1] !== null && dirname[0] !== null) {
    // let rr = "/src/views/demos/" + dirname[1] + "/Routes.ts";
    let routs = {
      path: "/demos/" + dirname[1],
      meta: {
        requiresAuth: true,
        layout: "ui",
        category: "DEMOS",
      },
      component: () => import(dirname[0]),
      children: [] as Array<{
        path: string;
        redirect?: string;
        name?: string;
        component?: () => Promise<any>;
      }>,
    };
 


    let pagesroutes: Array<{
      path: string;
      redirect?: string;
      name?: string;
      component?: () => Promise<any>;
    }> = [
      {
        path: "",
        redirect: "/demos/" + dirname[1] + "/main",
      },
    ];
 
    const pageR = import.meta.glob("/src/views/demos/*/pages/*.vue", {
      eager: true,
    });

    Object.keys(pageR).forEach((item) => {
 
      const pagesDirRegex = new RegExp(`\\/src\\/views\\/demos\\/${dirname[1]}\\/pages\\/(\\S*)\\.vue`);
      const matchResult = item.match(pagesDirRegex);
      if (matchResult) {
        // 匹配到.vue文件路径
        // console.log(matchResult[0]);
        // 进一步处理该路径
        let pagesroute = {
          path: matchResult[1],
          component: () => import(matchResult[0]),
          name: dirname[1] +'-'+matchResult[1],
        };
  
        pagesroutes.push(pagesroute);
      }
 
    });
    routs.children = pagesroutes;
    routes.push(routs);
  }
});
export default routes;```

@yangjiakai
Copy link
Owner

这个我先看一下,通过子目录生成侧边栏的话,我觉得还是nuxt3版本添加这个功能比较合适,这个项目说实话pages页面比较混乱且数量多,实现这个成本巨大

@cgisky1980
Copy link
Author

@yangjiakai 我自己使用的话 pages 都清理了。 目前的项目结构,要清理或者添加新的页面 都很麻烦,
需要 locales 中N个文件处理 、router 处理 、 navigation.ts 还有 menu中处理。 nuxt3版本 就很清爽

以后再做迁移吧

@yangjiakai
Copy link
Owner

@cgisky1980
页面清理之后精简版也发不了
这几天在集中完善nuxt3版本,一边进行迁移一边也在重构,这段时间重心可能会放在nuxt3版本了,自动生成侧边栏,我抽空添加到nuxt版本上吧

@cgisky1980
Copy link
Author

nuxt3 好像本身就带自动路由了

@cgisky1980
Copy link
Author

我自己写的那玩意 dev可以 打包的时候 那几个VUE没编译进去 还是用vite-plugin-pages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants