我有一个图标组件,可以使用它获取 SVG 路径
get absUrl() {
return window.location.href.split('#')[0];
}
但问题是,如果您登陆未“找到”的页面并且发生重定向
图标停止工作并且检查代码显示它为每个图标放置的 URL 是错误的 URL
例如,在不存在的“登录”页面上查找“仪表板”图标
网址是
https://localhost:4200/login#dashboard
但我在页面上
https://localhost:4200/404
所以链接应该是
https://localhost:4200/404#dashboard
有什么想法如何保持同步吗?与我所在的页面?
否则,一旦由于找不到页面而重定向到 /404,我的所有图标都会停止工作?
EDIT:
以下是在组件模板上使用 absUrl 的方式:
<svg
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
[style.fill]="color ? color : ''"
[style.width]="size"
[style.height]="size"
>
<use [attr.xlink:href]="absUrl + '#' + name"></use>
</svg>
EDIT 2
作为临时解决方法,我从以下位置配置了 404 重定向:
{ path: '**', redirectTo: '404'},
to
{ path: '**', component: NotFoundComponent },
但是人们可能会链接到不存在的路由,因此如果检测到路由不是“404”,我会尝试从组件本身运行重定向。
当在构造函数、ngOnInit 甚至 ngOnViewInit 中调用时,这不起作用
但是,如果在任何地方(包括构造函数)在 setTimeout 内以 0 毫秒调用——这将正常工作并呈现。
constructor(public router: Router) {
if (window.location.href.slice(window.location.href.length - 3) !== '404') {
setTimeout(() => {
this.router.navigate(['404']);
}, 0);
}
}
这让我觉得这是 Angular 本身的一些渲染错误。这有点hacky和无法解释,但有效......