gpt4 book ai didi

Angular 路由 - 避免硬编码字符串

转载 作者:太空狗 更新时间:2023-10-29 17:53:49 29 4
gpt4 key购买 nike

我有点担心在我的 Angular 应用程序中使用硬编码的路由字符串。只是好像有点不对劲!例如

 this._router.navigate(['dashboard/customer', customer.CustomerId]);
path: 'customer',
component: CustomerComponent,

有解决办法吗?

最佳答案

在路由模块中为路由器路径定义一个静态变量,然后在整个应用程序中使用它。例如:

定义路由路径:

  export class AppRoutes {
public static CUSTOMER: string = "customer";
public static ERROR: string = "error";
}

路由配置:

const routes: Routes = [ 
{
path: AppRoutes.CUSTOMER, component: CustomerComponent
}
];

导航:

this._router.navigate(["/" + AppRoutes.CUSTOMER, customer.CustomerId]);

关于Angular 路由 - 避免硬编码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47431775/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com