如何将 EventEmitter 输出附加到路由下的组件? [英] How do you attach an EventEmitter Output to a component under routing?

查看:25
本文介绍了如何将 EventEmitter 输出附加到路由下的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个路由配置

@RouteConfig([
    { path: '/', name: 'Start', component: Journal },
    { path: '/register', name: 'Register', component: Register },
    { path: '/login', name: 'Login', component: Login },
    { path: '/settings', name: 'Settings', component: Settings },
])

我想收听从 Journal 的 @Output 发出的事件.当我在模板中唯一的引用是

I'd like to listen to events emitted from Journal's @Output. How can I do that when the only reference I have in the template is

<a class="nav-item nav-link" [routerLink]="['Journal']">Journal</a>

?

推荐答案

我意识到这现在已经很老了,但是将状态传递给路由是可以撤消的.这主要是因为 routerLink 是通用的,可以接收任意数量的潜在子节点.因此,即使您可以传递状态或处理其事件,最终也会带来不必要的复杂性.

I realize this is pretty old now, but passing state to the route is undoable. This is mostly because routerLink is generic and could take any number of potential children. So even if you could pass state or process its events, you would end up with unneeded complexity.

管理父子组件之间状态的正确方法是共享服务.基本上,它归结为以下步骤:

The correct way to manage state between parent and child components, is a shared service. Essentially, it comes down to these steps:

  1. 编写服务.
  2. 不是从您的模块提供服务,而是从父组件提供服务(使其无法被外部代码访问).
  3. 将服务注入父级和任何需要它的子级.

这对于传递给子进程的状态和从子进程收到的通知同样有效,并且不会污染路由设置.

This would work equally well with state passed into the child and with notifications received from the child and doesn't pollute the route setup.

这篇关于如何将 EventEmitter 输出附加到路由下的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆