【发布时间】:2019-02-04 22:59:39
【问题描述】:
我的 Angular 组件中有标题和正文。
在标题中有一个通知计数,并且希望在正文中的用户更改计数时更新计数。
app.routing.ts
export const AppRountes: Routes = [{
component: LayoutComponent,
children: [
{
path: 'notification',
loadChildrent: './notification/notification.module#NotificationModule'
}
]
}]
我有一个 NotificationComponent 用于通知。
通知图标和计数在 LayoutComponent 中。
这里,我想从 NotificationComponent 通知到 LayoutComponent。
我能够使用 @ViewChild 从 LayoutComponent 与 NotificationComponent 进行通信,但不知道如何从子项与父项进行通信。
【问题讨论】:
-
最好的方法是在组件之间使用共享服务。
-
目前这不是一个选项,因为它需要对现有代码进行大量更改。