在导航到 Angular 2 组件中的路由时传递 @Input 并订阅 @Output [英] Passing @Input and subscribing to @Output while navigating to a Route in Angular 2 Component

查看:24
本文介绍了在导航到 Angular 2 组件中的路由时传递 @Input 并订阅 @Output的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们导航到路由并加载组件时,

When we navigate to a route and load components,

  1. 我们可以在加载的子组件中传递用 @Input 修饰的变量吗?我们可以订阅用 @Output 修饰的 EventEmitter 吗?
  2. 在父组件中是否有可用的lifecycle Hook,其中定义了Route,我们可以获得对加载组件的引用,以便动态地将值\订阅函数传递给子组件?
  1. Can we pass variable decorated with @Input in loaded child component and can we subscribe to EventEmitter decorated with @Output ?
  2. Is there any lifecycle Hook available in parent, where Route is defined and we may get a reference to loaded component, so as to pass values\subscribe functions to child component dynamically?

父组件

   @Component({
    moduleId: module.id,
    selector: "parent",
    templateUrl: "<router-outlet></router-outlet>"
  })
  @Routes([
   { path: "/child-component1", component: Child1Component }
  ])
  export class Parent {
   // Can we pass var1 and subscribe close here of Child1Component when route is navigated dynamically based upon path?
   // Is there any lifecycleHook available in parent where Route is defined?
  }

子组件

  @Component({
    moduleId: module.id,
    selector: "child-component1",
    template: "<div>{{var1}}</div><button (click)='closeMenu()'>Close</button>"
  })
  export class Child1Component {
   @Input() var1: string;
   @Output() close: EventEmitter<any> = new EventEmitter<any>();

   constructor() { }

   closeMenu = (): void => {
      this.close.emit("");
   }
 }

我正在使用 Angular2 RC1

提前致谢!

推荐答案

只需使用共享服务进行组件之间的通信.另请参阅 https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

Just use a shared service for communication between components. See also https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

输入和输出不能用于路由组件.

Inputs and outputs can't be used for routed components.

这篇关于在导航到 Angular 2 组件中的路由时传递 @Input 并订阅 @Output的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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