【问题标题】:Pass callback to angular component create with route将回调传递给使用路由创建的角度组件
【发布时间】:2020-07-14 00:56:28
【问题描述】:

我有一个使用路由创建的组件,并且该组件有一个输出 EventEmitter。如果组件是在路由上创建的,我如何订阅此输出?

组件是这样创建的:

{ path: 'component-A',
  component: ComponentAComponent
}

【问题讨论】:

    标签: angular angular-component-router


    【解决方案1】:
    <new-component (event_emitter_variable_name)="custom_function()"></new-component>
    

    要了解更多信息,请访问example


    更新

    您可以创建服务以在两个组件之间进行通信。组件 2 将订阅服务中的事件。组件 1 将推送到服务中的此事件。 example.

    【讨论】:

    • 组件是用路由创建的。
    • 抱歉没看懂你的问题。我更新了答案。
    【解决方案2】:

    你可以从 router-outlet 监听激活的 EventEmitter

    <router-outlet (activate)="onRouterActivate($event)"></router-outlet>
    

    $event 是你的路由组件,然后你可以监听一个事件。根据您想要实现的目标,使用服务可能是更好的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2017-05-04
      • 1970-01-01
      • 2020-06-09
      • 2018-06-20
      • 2020-02-06
      • 1970-01-01
      • 2017-08-01
      相关资源
      最近更新 更多