且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查角度 2 中是否存在路线

更新时间:2023-11-27 16:00:58

没有办法检查路由路径是否存在于配置中,但是你可以这样做在路由器配置模块中使用 ** 的配置重定向.

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'}
];

或者在您的组件中执行此操作,

Or do this in your component,

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);
}