【问题标题】:Child to parent communication in Angular 6Angular 6中的孩子与父母的沟通
【发布时间】: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 进行通信,但不知道如何从子项与父项进行通信。

【问题讨论】:

  • 最好的方法是在组件之间使用共享服务。
  • 目前这不是一个选项,因为它需要对现有代码进行大量更改。

标签: angular angular6


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 2020-01-29
    • 2011-02-21
    • 1970-01-01
    • 2017-07-01
    • 2019-05-22
    • 1970-01-01
    • 2011-02-20
    • 2014-02-16
    相关资源
    最近更新 更多