【发布时间】:2018-08-05 06:37:40
【问题描述】:
我不确定这是否是实现此功能的最佳方式。如果您有其他意见,请分享。
我必须实现一个多收件箱系统,用户可以将多封电子邮件按不同的收件箱分组。
例如:http://localhost/inbox/personal/ 将在personal 收件箱中显示电子邮件列表,http://localhost/inbox/business/3 将在business 收件箱中显示电子邮件列表,并突出显示 ID 为:3 的电子邮件
路线如下所示:
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'personal' // redirect to personal inbox
},
{
path: ':inbox',
component: InboxContentComponent, // list of emails in :inbox
children: [
{
path: '',
component: InboxNoSelectedEmailComponent, // no email selected
},
{
path: ':id',
component: InboxSelectedEmailComponent, // show selected email content
}
]
}
];
我的问题是 InboxContentComponent 。我需要检测收件箱何时更改以及是否选择了电子邮件
constructor(private route: ActivatedRoute) {
}
ngOnInit() {
this.route.paramMap.subscribe(inbox => {
console.log('inbox', inbox);
});
}
仅在收件箱更改而不是电子邮件更改时才会发出事件。 有没有办法检测子路由参数何时发生变化?
这样做this.route.firstChild.paramMap.subscribe(); 仅在组件初始化时路由具有第一个子级时才有效。如果路线是这样的http://localhost/inbox/business/,那么this.route.firstChild就是null
我认为的解决方案是定义这样的路线
{
path: ':inbox/:id',
component: InboxContentComponent
}
然后检查InboxContentComponent是否设置了:id
有什么想法吗?
【问题讨论】:
-
我认为这个答案将为您提供可能的解决方案:stackoverflow.com/questions/48807518/…
标签: angular typescript angular-router