息子通信角度成分

1.コール親コンポーネントサブアセンブリ方法およびデータ - 入力デコレータ

 

1. fatherComponent.html中

  toChildStr - 親コンポーネントのデータに定義されています

  fatherMethod - メソッドの親コンポーネント

  fatherComponent - 親コンポーネントの例には、サブアセンブリに渡される[fatherCompoent]によって親コンポーネント自体が、これは、親コンポーネントインスタンスを指し

<childComponent #login [toChildStr] = "toChildStr" [fatherMethod] = "fatherMethod" [fatherComponent] = "この" > </ APP-ログイン>

2. childComponent.ts中

  

  デコレーター、メソッド、親コンポーネントの例で@Input親コンポーネントの属性を取得します。

輸出クラスLoginComponentは、実装のOnInitを{
  @ 入力()toChildStr 文字列-親コンポーネントのプロパティ
  @ 入力()fatherMethod 任意の-親コンポーネント方法
  @ 入力()fatherComponent 任意-親コンポーネントの例
  コンストラクタ(){}
  ngOnInit (){
  }
  getFatherComponentMethod (){
    アラートこれtoChildStr ) - -親コンポーネントのプロパティ
    アラートこれfatherComponent toChildStr ) - -親親コンポーネントの属性取得コンポーネントインスタンス
    このfatherComponent fatherMethod () - -親コンポーネントのメソッド呼び出しの親コンポーネントの例
    このfatherMethod () - -メソッドの親コンポーネント
  }
}

 

2.動員親コンポーネント及びサブアセンブリのデータの方法 - ViewChildデコレータ

  

  1. fatherComponent.html中

  マーカーサブアセンブリに#var方法により、

<childComponent #child > </ childComponent>

  2. fatherComponent.ts中

  通过@ViewChild()装饰器获取子组件实例

 @ViewChild('child', null) childComponent: any
  userChildMethod() {
    // 调用子组件方法 -- 获取属性同样写法
    this.childComponent.childMethod()
  }

3. 通过事件触发器,子组件给父组件传参。即父组件获取子组件数据

以上两种方式都是主动获取数据或者方法。这种方式通过 EventEmitter 和 outPut 方式相当于,通过事件,子组件广播数据给父组件,父组件被动接受参数。

  1. childComponent.ts

  

import { Output, EventEmitter } from '@angular/core';
export class LoginComponent implements OnInit {
  @Output() outer:any = new EventEmitter() -- 声明并且输出一个事件触发器
  public msg = "123"
  public obj = {
    num: 1
  }
  sendParent() {
    this.outer.emit(this.obj) -- 通过事件触发器,发送数据给父组件
  }

  2. childComponent.html

  

  <h5>通过事件触发器,子组件向父组件广播数据</h5>
  <button (click)="sendParent()">数据发送给父组件</button>

  3. parentComponent.html,

通过 (事件触发器)= “方法” 的方式监听子组件发出父消息。 (事件触发器) 必须与子组件中定义的名称一致

  

  <childComponent (outer)="onReciveChildMsg($event)"></childComponent>

  

  4. parentComponent.ts

  通过方法获取传递的参数 $event

  onReciveChildMsg(e) {
      alert(e.num)
      e.num = 2
      // e = "456"
      // this.msg = e
    }

 

おすすめ

転載: www.cnblogs.com/monkey-K/p/11587382.html