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

查看:57
本文介绍了如何在布线下将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天全站免登陆