在路径路径的开头(角度)添加子路径

时间:2020-07-28 20:11:25

标签: javascript angular typescript routes

我有一个角度项目,当用户输入www.mysite.com/hisName之类的URL时,需要这个名称并将其添加到项目中的所有路由页面中,最终结果将像www.mysite.com/hisName/home

这是我的路由代码

    import { NgModule } from "@angular/core";
    import { Routes, RouterModule } from "@angular/router";
    import { AppComponent } from "./app.component";

    const routes: Routes = [
    { path: "", redirectTo: "home", pathMatch: "full" },
    { path: "home", component: HomeComponent },
     path: "supliers",
     loadChildren: () =>
       import("./supliers-container/supliers-container.module").then(
     (mod) => mod.SupliersContainerModule
     ),
  }


 @NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: true })],
   exports: [RouterModule],
  })
   export class AppRoutingModule {}

1 个答案:

答案 0 :(得分:0)

我认为您正在要求使用通配符选项?必须将其定义为最后一个元素。

const routes: Routes = [
    { path: "", redirectTo: "home", pathMatch: "full" },
    { path: "home", component: HomeComponent },
    { path: "supliers",
     loadChildren: () =>
       import("./supliers-container/supliers-container.module").then(
          (mod) => mod.SupliersContainerModule
        ),
     },

     { path "**", redirectTo: "home", pathMatch: "full" } // here
]

注意:如果添加通配符,我认为您不需要第一个路径选项。

相关问题