首页 > 解决方案 > Angular 6中的孩子与父母的沟通

问题描述

我的 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 进行通信,但不确定如何从子项与父项进行通信。

标签: angularangular6

解决方案


通常,您会使用EventEmitterplus @Output,因为您使用的是路由器,所以您不能真正做到这一点。我想你可以在这里找到答案:Angular 2 output from router-outlet


推荐阅读