检查角度2中是否存在路线
问题描述:
我想检查角度项目中是否存在路线.
例如,URL栏中的用户类型http://localhost:4200/#/timestamp
而项目中不存在timestamp
,如何在不进行重定向的情况下进行检查?
I want to check if a route exists in an angular project.
For example user types http://localhost:4200/#/timestamp
in the url bar and timestamp
does not exist in the project, how will you be able to check without redirecting?
答
如果路由路径存在于配置中,则为 no way to check
,但是您可以使用在配置中进行重定向**
在路由器配置模块中.
There is no way to check
if the route path exists in the config, however you can do a redirect in configuration using **
in router config module.
export const AppRoutes = [
{ path: "", redirectTo: "home", pathMatch: "full" },
{ path: '**', redirectTo: 'home'}
];
或者在您的组件中执行
string redirectUrl = "http://localhost:4200/#/timestamp";
this.redirectUrl = this.redirectUrl ? this.redirectUrl : '/home';
this.router.navigate([this.redirectUrl]);
或者如果要遍历所有已配置的路由,则可以从router.config获取路由
or if you want to loop over all the configured routes, you can get the routes from router.config
for (var i = 0; i < this.router.config.length; i++) {
var routePath:string = this.router.config[i].path;
console.log(routePath);
}