我有一个角度项目,当用户输入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 {}
答案 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
]
注意:如果添加通配符,我认为您不需要第一个路径选项。