【问题标题】:add sub Route in the beginning of route path (angular)在路线路径的开头添加子路线(角度)
【发布时间】:2020-07-28 20:11:25
【问题描述】:

我有 Angular 项目,当用户键入 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 {}

【问题讨论】:

    标签: javascript angular typescript routes


    【解决方案1】:

    我认为您要求的是通配符选项?它必须被定义为最后一个元素。

    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
    ]
    

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

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2021-01-28
      相关资源
      最近更新 更多