美文网首页Angular 4.X+
组件间传值之@Output

组件间传值之@Output

作者: IT飞牛 | 来源:发表于2018-04-24 23:32 被阅读0次

对应@input输入传值,也有@Output输出传值。
输入传值请我之前写的一篇文章:https://www.jianshu.com/p/686294cb2b29

例如:
A是B的父组件,B中有Input参数how,A中调用B组件,并传入[Import]参数。B根据传入的Import参数,发射返回相应的数据。

//A页面代码
    <table class="table table-bordered">
      <tbody>
      <tr>
       ......
      </tr>
      <tr *ngFor="let item of tasks|filter:'title':keyword;let i=index">
        ......
        <td>
          <app-jinji [Import]="item.jinji" (Export)="print($event)"></app-jinji>
        </td>
       ......
      </tr>
      </tbody>
    </table>
//A中ts代码,定义print方法
......
  print(data) {
    console.log(data);
  }
......
//B中ts代码
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';

@Component({
  selector: 'app-jinji',
  templateUrl: './jinji.component.html',
  styleUrls: ['./jinji.component.css']
})
export class JinjiComponent implements OnInit {
  private shenheren: string;

  @Input('Import')
  private how: Number;

  @Output('Export')
  EmitData: EventEmitter<string> = new EventEmitter();

  constructor() {

  }

  ngOnInit() {
    // console.log('how:' + this.how);
    switch (this.how) {
      case 1:
        this.shenheren = '马云';
        // console.log(1);
        break;
      case 2:
        this.shenheren = '李彦宏';
        // console.log(2);
        break;
      case 3:
        this.shenheren = '马化腾';
        // console.log(3);
        break;
      case 4:
        this.shenheren = '柳传志';
        // console.log(4);
        break;
      default:
        this.shenheren = '贾跃亭';
        // console.log(5);
        break;
    }
    this.EmitData.emit(this.shenheren);
  }
}

运行后,页面console窗口中会打印出各位大佬的姓名,如图:


image.png

相关文章

  • 组件间传值之@Output

    对应@input输入传值,也有@Output输出传值。输入传值请我之前写的一篇文章:https://www.jia...

  • 组件基础

    组件的入门 组件的私有化 组件的切换 组件间的传值-向组件内传值 组件间传值-向组件外传值 ref获取元素

  • vue 6种通信总结①

    常用vue通信大概有这几种方式进行: 组件间的父子之间的传值 组件间的子父之间的传值 非组件间的组件间的传值(简称...

  • (Vue-cli)六、组件间传值(组件间传值&全局状态管理sto

    六、组件间传值 1.父子组件传值 (1) 父传子 父组件向子组件传值,可以通过绑定属性传值;子组件通过props接...

  • 【Vue3 从入门到实战 进阶式掌握完整知识体系】011-探索组

    2、组件间传值及传值校验 父组件给子组件传值 运行结果 父组件给子组件传动态参数 运行结果 子组件校验父组件的传参...

  • vue通信、传值的多种方式

    组件之间传值方式 页面间之间传值方式

  • Vue和React组件通信的总结

    在现代的三大框架中,其中两个Vue和React框架,组件间传值方式有哪些? 组件间的传值方式 组件的传值场景无外乎...

  • uni-app通信方式

    一,组件间传值包括下面三种情况: 1,父组件给子组件传值 2,子组件给父组件传值 3,兄弟组件通讯 二 代码1 父...

  • Vue 组件传值

    一、组件间通信传值的各种方式与场景 1、父组件向子组件(跨级)传值 1.1 父组件通过props给子组件传值 1....

  • Vue组件间通信

    Vue组件间通信 父子组间通信 通过props向子组件传值,通过事件向父级组件发送消息 通过props向子组件传值...

网友评论

    本文标题:组件间传值之@Output

    本文链接:https://www.haomeiwen.com/subject/hugslftx.html