我不确定这是否是实现此目的的最佳方法。如果您有其他意见请分享。
我必须实现一个多收件箱系统,用户可以将多封电子邮件按不同的收件箱分组。
例如:http://localhost/inbox/personal/
将显示电子邮件列表personal
inbox, 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/
then this.route.firstChild
is null
关于解决方案,我认为是定义这样的路线
{
path: ':inbox/:id',
component: InboxContentComponent
}
然后登记入住InboxContentComponent
if :id
是否设置
有任何想法吗?