vue路线映射与重定向

发布于 2025-01-22 15:18:38 字数 6338 浏览 0 评论 0原文

我希望这是一个快速的。 我有这个看起来像这样的映射:

const routes = routeOptions.map((route) => {
  return {
    children: route?.children?.map((child) => {
      return {
        ...{
          path: child.path,
          name: child.name,
          meta: child.meta,
          alias: child.alias,
        },
        component: () =>
          import(
            /* webpackPrefetch: true */
            /* webpackChunkName: "[request]" */
            `../views/${child.name
                  .replace(" ", "-")
                  .toLowerCase()}/${child.name
                  .replace(" ", "-")
                  .toLowerCase()}.component.vue`
          ),
      };
    }),
    ...{
      path: route.path,
      name: route.name,
      meta: route.meta,
      redirect: route.redirect,
    },
    component: () =>
      import(
        /* webpackChunkName: "[request]" */
        `../views/${route.name
              .replace(" ", "-")
              .toLowerCase()}/${route.name
              .replace(" ", "-")
              .toLowerCase()}.component.vue`
      ),
  };
});

然后我可以设置 RouteOptions 这样:

const routeOptions: RouteOptions[] = [{
    path: "/account",
    name: "account",
    meta: {
      title: "Account",
    },
    redirect: {
      name: "sign-in",
    },
    children: [{
        path: "sign-in",
        name: "sign-in",
        meta: {
          title: "Sign in",
        },
      },
      {
        path: "join",
        name: "join",
        meta: {
          title: "Join",
        },
      },
      {
        path: "add-password",
        name: "add-password",
        meta: {
          title: "Add password",
        },
      },
      {
        path: "forgot-password",
        name: "forgot-password",
        meta: {
          title: "Forgot password",
        },
      },
      {
        path: "reset-password",
        name: "reset-password",
        meta: {
          title: "Reset password",
        },
      },
    ],
  },
  {
    path: "/crew",
    name: "staff",
    meta: {
      title: "Crew",
    },
    redirect: {
      name: "staff-account",
    },
    children: [{
        path: "account",
        name: "staff-account",
        meta: {
          title: "Account",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "demos",
        name: "staff-demos",
        meta: {
          title: "Demos",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "leader-boards",
        name: "staff-leader-boards",
        meta: {
          title: "Leader boards",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "search",
        name: "staff-search",
        meta: {
          title: "Search",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "stories",
        name: "staff-stories",
        meta: {
          title: "Stories",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
    ],
  },
  {
    path: "/404",
    name: "not-found",
    meta: {
      title: "Page not found",
    },
  },
  {
    path: "*",
    name: "layout",
    meta: {},
    children: [{
        path: "/business/live-better",
        name: "blog",
        meta: {
          title: "Partner Blog",
        },
        children: [{
          path: ":title",
          name: "blog",
        }, ],
      },
      {
        path: "/business/live-better/:title",
        name: "blog-post",
      },
      {
        path: "/brands",
        name: "brand-list",
        meta: {
          title: "Brands",
        },
      },
      {
        path: "/brands/:brandSlug",
        name: "brand-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/categories",
        redirect: {
          name: "categories"
        },
      },
      {
        path: "/categories/:categorySlug",
        redirect: {
          name: "product-list"
        },
      },
      {
        path: "/categories/:categorySlug/:productId/:productTitle",
        redirect: {
          name: "product-details"
        },
      },
      {
        path: "/products",
        name: "categories",
        meta: {
          title: "Products",
        },
      },
      {
        path: "/products/:categorySlug",
        name: "product-list",
        meta: {
          title: "Products",
        },
      },
      {
        path: "/products/:categorySlug/:productId/:productTitle",
        name: "product-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/favourites",
        name: "favourites",
        meta: {
          title: "Your favourites",
        },
      },
      {
        path: "/feedback",
        name: "consumer-feedback",
        meta: {
          title: "Your feedback",
          authorize: [],
        },
      },
      {
        path: "/venues/:venueSlug/theatres",
        name: "theatre-list",
        meta: {
          title: "Theatres",
        },
      },
      {
        path: "/venues/:venueSlug/theatres/:theatreSlug",
        name: "theatre-details",
        meta: {
          title: "Theatre",
        },
      },
      {
        path: "/venues",
        name: "venue-list",
        meta: {
          title: "Venues",
        },
      },
      {
        path: "/venues/:venueSlug",
        name: "venue-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/search",
        name: "search",
        meta: {
          title: "Search results",
        },
      },
      {
        path: "*",
        name: "home",
        meta: {
          title: "Home",
        },
      },
    ],
  },
];

其中大多数工作,但是如果您在此处查看此部分:

{
  path: "/categories",
  redirect: { name: "categories" },
},
{
  path: "/categories/:categorySlug",
  redirect: { name: "product-list" },
},
{
  path: "/categories/:categorySlug/:productId/:productTitle",
  redirect: { name: "product-details" },
},

我希望将它们重定向到其他命名视图。 我的映射目前的工作方式是,它以 views 文件夹中的匹配模板中的名称和外观,但这些路由没有模板,因为它们只是重定向。 有谁知道我可以对我的代码做些什么才能使其与之合作?

I hope this is a quick one.
I have this mapping that looks like this:

const routes = routeOptions.map((route) => {
  return {
    children: route?.children?.map((child) => {
      return {
        ...{
          path: child.path,
          name: child.name,
          meta: child.meta,
          alias: child.alias,
        },
        component: () =>
          import(
            /* webpackPrefetch: true */
            /* webpackChunkName: "[request]" */
            `../views/${child.name
                  .replace(" ", "-")
                  .toLowerCase()}/${child.name
                  .replace(" ", "-")
                  .toLowerCase()}.component.vue`
          ),
      };
    }),
    ...{
      path: route.path,
      name: route.name,
      meta: route.meta,
      redirect: route.redirect,
    },
    component: () =>
      import(
        /* webpackChunkName: "[request]" */
        `../views/${route.name
              .replace(" ", "-")
              .toLowerCase()}/${route.name
              .replace(" ", "-")
              .toLowerCase()}.component.vue`
      ),
  };
});

Then I can set my routeOptions like this:

const routeOptions: RouteOptions[] = [{
    path: "/account",
    name: "account",
    meta: {
      title: "Account",
    },
    redirect: {
      name: "sign-in",
    },
    children: [{
        path: "sign-in",
        name: "sign-in",
        meta: {
          title: "Sign in",
        },
      },
      {
        path: "join",
        name: "join",
        meta: {
          title: "Join",
        },
      },
      {
        path: "add-password",
        name: "add-password",
        meta: {
          title: "Add password",
        },
      },
      {
        path: "forgot-password",
        name: "forgot-password",
        meta: {
          title: "Forgot password",
        },
      },
      {
        path: "reset-password",
        name: "reset-password",
        meta: {
          title: "Reset password",
        },
      },
    ],
  },
  {
    path: "/crew",
    name: "staff",
    meta: {
      title: "Crew",
    },
    redirect: {
      name: "staff-account",
    },
    children: [{
        path: "account",
        name: "staff-account",
        meta: {
          title: "Account",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "demos",
        name: "staff-demos",
        meta: {
          title: "Demos",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "leader-boards",
        name: "staff-leader-boards",
        meta: {
          title: "Leader boards",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "search",
        name: "staff-search",
        meta: {
          title: "Search",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
      {
        path: "stories",
        name: "staff-stories",
        meta: {
          title: "Stories",
          authorize: ["Venue Staff", "Venue Manager"],
        },
      },
    ],
  },
  {
    path: "/404",
    name: "not-found",
    meta: {
      title: "Page not found",
    },
  },
  {
    path: "*",
    name: "layout",
    meta: {},
    children: [{
        path: "/business/live-better",
        name: "blog",
        meta: {
          title: "Partner Blog",
        },
        children: [{
          path: ":title",
          name: "blog",
        }, ],
      },
      {
        path: "/business/live-better/:title",
        name: "blog-post",
      },
      {
        path: "/brands",
        name: "brand-list",
        meta: {
          title: "Brands",
        },
      },
      {
        path: "/brands/:brandSlug",
        name: "brand-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/categories",
        redirect: {
          name: "categories"
        },
      },
      {
        path: "/categories/:categorySlug",
        redirect: {
          name: "product-list"
        },
      },
      {
        path: "/categories/:categorySlug/:productId/:productTitle",
        redirect: {
          name: "product-details"
        },
      },
      {
        path: "/products",
        name: "categories",
        meta: {
          title: "Products",
        },
      },
      {
        path: "/products/:categorySlug",
        name: "product-list",
        meta: {
          title: "Products",
        },
      },
      {
        path: "/products/:categorySlug/:productId/:productTitle",
        name: "product-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/favourites",
        name: "favourites",
        meta: {
          title: "Your favourites",
        },
      },
      {
        path: "/feedback",
        name: "consumer-feedback",
        meta: {
          title: "Your feedback",
          authorize: [],
        },
      },
      {
        path: "/venues/:venueSlug/theatres",
        name: "theatre-list",
        meta: {
          title: "Theatres",
        },
      },
      {
        path: "/venues/:venueSlug/theatres/:theatreSlug",
        name: "theatre-details",
        meta: {
          title: "Theatre",
        },
      },
      {
        path: "/venues",
        name: "venue-list",
        meta: {
          title: "Venues",
        },
      },
      {
        path: "/venues/:venueSlug",
        name: "venue-details",
        meta: {
          title: "Details",
        },
      },
      {
        path: "/search",
        name: "search",
        meta: {
          title: "Search results",
        },
      },
      {
        path: "*",
        name: "home",
        meta: {
          title: "Home",
        },
      },
    ],
  },
];

Most of these work, but if you take a look at this section here:

{
  path: "/categories",
  redirect: { name: "categories" },
},
{
  path: "/categories/:categorySlug",
  redirect: { name: "product-list" },
},
{
  path: "/categories/:categorySlug/:productId/:productTitle",
  redirect: { name: "product-details" },
},

I want these to redirect to other named views.
The way my mapping works at the moment, is that it takes the name and looks in the views folder for a matching template, but these routes don't have templates because they are just redirects.
Does anyone know what I can do to my code to get it to work with these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-29 15:18:38

我这样做了:

const createChildRoutes = (children) => {
  return children?.map((child) => {
    const childRoute = {
      path: child.path,
      name: child.name,
      meta: child.meta,
      alias: child.alias,
      redirect: child.redirect,
    };

    if (child.name) {
      return {
        ...childRoute,
        component: () =>
          import(
            /* webpackPrefetch: true */
            /* webpackChunkName: "[request]" */ `../views/${child.name
              .replace(" ", "-")
              .toLowerCase()}/${child.name
              .replace(" ", "-")
              .toLowerCase()}.component.vue`
          ),
      };
    } else {
      return childRoute;
    }
  });
};

const routes = routeOptions.map((route) => {
  const mainRoute = {
    path: route.path,
    name: route.name,
    meta: route.meta,
    alias: route.alias,
    redirect: route.redirect,
  };

  if (route.name) {
    return {
      children: createChildRoutes(route?.children),
      ...mainRoute,
      component: () =>
        import(
          /* webpackPrefetch: true */
          /* webpackChunkName: "[request]" */ `../views/${route.name
            .replace(" ", "-")
            .toLowerCase()}/${route.name
            .replace(" ", "-")
            .toLowerCase()}.component.vue`
        ),
    };
  } else {
    return {
      children: createChildRoutes(route?.children),
      ...mainRoute,
    };
  }
});

I did it like this:

const createChildRoutes = (children) => {
  return children?.map((child) => {
    const childRoute = {
      path: child.path,
      name: child.name,
      meta: child.meta,
      alias: child.alias,
      redirect: child.redirect,
    };

    if (child.name) {
      return {
        ...childRoute,
        component: () =>
          import(
            /* webpackPrefetch: true */
            /* webpackChunkName: "[request]" */ `../views/${child.name
              .replace(" ", "-")
              .toLowerCase()}/${child.name
              .replace(" ", "-")
              .toLowerCase()}.component.vue`
          ),
      };
    } else {
      return childRoute;
    }
  });
};

const routes = routeOptions.map((route) => {
  const mainRoute = {
    path: route.path,
    name: route.name,
    meta: route.meta,
    alias: route.alias,
    redirect: route.redirect,
  };

  if (route.name) {
    return {
      children: createChildRoutes(route?.children),
      ...mainRoute,
      component: () =>
        import(
          /* webpackPrefetch: true */
          /* webpackChunkName: "[request]" */ `../views/${route.name
            .replace(" ", "-")
            .toLowerCase()}/${route.name
            .replace(" ", "-")
            .toLowerCase()}.component.vue`
        ),
    };
  } else {
    return {
      children: createChildRoutes(route?.children),
      ...mainRoute,
    };
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文