angular - url参数错误时如何触发通配符路由?
问题描述
在 Angular 6 中,我使用 observable 来管理基于 URL 参数获取详细记录,如下所示:
ngOnInit() {
this.hero$ = this.route.paramMap.pipe(
switchMap((params: ParamMap) => {
const id = parseInt(params.get('id'), 10);
if (!id) {
throw new Error('Hero id invalid.');
}
return this.service.getHeroById(id);
}),
catchError(err => {
this.router.navigate(['/pageNotFound'], {skipLocationChange: true});
return throwError(err);
}),
);
}
在 catchError 管道中,我只是导航到一个不存在的 URL,这会导致通配符路由触发并显示我的 PageNotFoundComponent。这似乎真的很令人费解,但我不明白如何让路由器显示该组件。有没有更好的办法?
解决方案
推荐阅读
- angular - 无限循环上调用的 Firebase 云函数
- linux - 使用'at'命令在第二天录制视频流并且只做一次
- google-sheets - Google表格中QUERY内的IF语句
- powershell - 通过 netcat 传输文件作为 Powershell 中的后台作业
- c# - c#在幻灯片上插入多个形状组时出错
- ruby-on-rails - 如果在类主体中调用了方法,如何在 Ruby/Rails 中进行测试?
- web-scraping - Linux 中的 Scrapy Splash + Crawlera 总是出现 503 服务不可用错误
- android - 您如何将数据从当前活动传递到以前的活动
- regex - 地址的 MS Word 正则表达式
- django - Django:SendGrid 点击跟踪服务阻止消息框架工作