检查角度2中是否存在路线 [英] check if a route exist in angular 2

查看:63
本文介绍了检查角度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);
}

这篇关于检查角度2中是否存在路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆