寻找以下问题的解决方案:
当用户手动导航到一个url,例如www.website.com/logi并输入错误时,我希望有一些逻辑来导航到正确的.../login url。我的网站有大约10个页面,它们有不同的导航urls,我也想检查那里的拼写错误。逻辑,可以检查1-2个字符的差异,并智能导航用户,即使他们犯了打字错误。
什么是这个问题的好解决方案?
发布于 2021-07-31 08:21:00
直接从Angular文档中可以看出,UrlMatcher是最适合你需要的东西。
链接:https://angular.io/api/router/UrlMatcher
您现在可以根据需要修改/编写RegExp。
发布于 2021-07-31 08:12:06
在所有其他路由之后添加一个覆盖所有路由。在其中,检查您是否应该重定向。如果没有,则显示404 not Found错误。
// Routing module
const routes = [
// Other routes first
{ path: '**', component: NotFoundComponent }
];// NotFoundComponent
constructor(private route:ActivatedRoute, private router:Router) { }
ngOnInit(): void {
const redirects: [string, string[]][] = [
['logi', ['/login']]
];
for (const redirect of redirects) {
if (
new RegExp(redirect[0], 'i').test(
this.route.snapshot.url[0]?.path || ''
)
) {
// If on Universal server, set Response status to 301
this.router.navigate(redirect[1]);
return;
}
}
// Show 404 error
// If on Universal server, set Response status to 404
}https://stackoverflow.com/questions/68599749
复制相似问题