路由器出口的Angular 2输出 [英] Angular 2 output from router-outlet

查看:116
本文介绍了路由器出口的Angular 2输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从在路由器出口内渲染的子组件进行导航. 我的父组件具有路由器配置,我想在某些事件上手动导航.但是我不知道如何从子级向父级传递一些数据(用于导航)而没有输出.因为这种构造无法正常工作

I want to make navigation from child components that render inside router-outlet. My parent component have a router config and I want to navigate manually on some event. But I don't know how I can pass from child to parent some data (for navigation) without output. Because this construction is non working

 <router-outlet (navigateTo)="navigateToMessagePart($event)"></router-outlet>

我该如何以正确的方式做到这一点?也许从孩子那里导航?但是我怎样才能从孩子那里获得父方法. 非常感谢您的帮助!

How I can do it in right way? Maybe navigate it from child? But how I can get parent methods from child. Many thanks for any help!

推荐答案

<router-outlet></router-outlet>只是用于添加路由组件的占位符.不支持任何形式的绑定.

<router-outlet></router-outlet> is just a placeholder for adding routed components. There is no support for any kind of binding.

您可以创建一个自定义<router-outlet>,使您可以执行此操作或更常见的操作,可以使用共享服务在父组件和路由组件之间进行通信.

You can create a custom <router-outlet> that allows you to do that or more common, use a shared service to communicate between parent component and routed component.

有关更多详细信息,请参见 https://angular.io/docs/ts/latest/cookbook/component-communication.html

For more details see https://angular.io/docs/ts/latest/cookbook/component-communication.html

更新

现在有一个事件,可以获取添加的组件

There is now an event that allows to get the added component

<router-outlet (activate)="componentAdded($event)" (deactivate)="componentRemoved($event)"></router-outlet>

允许与componentAdded()

共享服务是首选方式.

这篇关于路由器出口的Angular 2输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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