如果不存在,请捕获角度通配符路由

时间:2022-10-30 18:07:46

In my angular project, i'm resolving most of the routes from a wildcard

在我的角度项目中,我正在通过通配符解析大部分路线

function getTemplateUrl (route) {
  return '/views/' + route.page + '.html';
}
.when('/:page', { templateUrl : getTemplateUrl })
.when('/:page/:edit', { templateUrl : getTemplateUrl })
.otherwise({redirectTo: '/dashboard'});

the only issue, is that when the route doesn't exist, it will still try to load the page, not the otherwise function and loads the page over and over again, thus creating an infinite loop.

唯一的问题是,当路径不存在时,它仍会尝试加载页面,而不是其他功能并反复加载页面,从而创建一个无限循环。

Is there any way I can check if the route exists?

有什么方法可以检查路线是否存在?

1 个解决方案

#1


0  

When you want to redirect all other pages to dashboard, you should specify dashboard route before otherwise

如果要将所有其他页面重定向到仪表板,则应先指定仪表板路径

function getTemplateUrl (route) {
  return '/views/' + route.page + '.html';
}
.when('/:page', { templateUrl : getTemplateUrl })
.when('/:page/:edit', { templateUrl : getTemplateUrl })
.when('/dashboard', { templateUrl : getTemplateUrl })
//add dashboard route before otherwise
.otherwise({redirectTo: '/dashboard'});

#1


0  

When you want to redirect all other pages to dashboard, you should specify dashboard route before otherwise

如果要将所有其他页面重定向到仪表板,则应先指定仪表板路径

function getTemplateUrl (route) {
  return '/views/' + route.page + '.html';
}
.when('/:page', { templateUrl : getTemplateUrl })
.when('/:page/:edit', { templateUrl : getTemplateUrl })
.when('/dashboard', { templateUrl : getTemplateUrl })
//add dashboard route before otherwise
.otherwise({redirectTo: '/dashboard'});