[Angular] 笔记 9:list/detail 页面以及@Output

1. @Output

input 好比重力,向下传递数据,list 传给 detail,smart 组件传给 dumb 组件,父组件传给子组件。input 顾名思义,输入数据给组件。

output 与之相反,好比火箭,向上传递数据或事件。output 顾名思义就是将某些数据发送出去。

语法:

ts 复制代码
@Output()
remove: EventEmitter<any> = new EventEmitter();

onRemove() {
    // 通过 emit 关键字向上发送事件
    this.remove.emit(this.detail);
}

handleRemove 函数里使用了 filter,是为了避免直接修改 state。

2. 实现删除功能的代码举例

2.1 pokemon-detail.component.html

pokemon-detail.component.html 中增加一个用来删除 Pokemon 的 button:

html 复制代码
<tr>
  <td class="pokemon-td" [class.cool-bool]="detail.isCool">
    {{ detail.id }} : {{ detail.name }}
    {{ detail.isCool == true ? "is COOL" : "is NOT COOL" }}
  </td>
  <!-- add a button -->
  <button (click)="onRemove()">Remove Pokemon</button>
</tr>

2.2 pokemon-detail.component.ts

pokemon-detail.component.ts 中增加 @Output, 以及 onRemove

ts 复制代码
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Pokemon } from 'src/app/models/pokemon';

@Component({
  selector: 'app-pokemon-detail',
  templateUrl: './pokemon-detail.component.html',
  styleUrls: ['./pokemon-detail.component.css'],
})
export class PokemonDetailComponent implements OnInit {

  @Input()
  detail!: Pokemon; 

  // 新增代码
  @Output()
  remove: EventEmitter<any> = new EventEmitter();

  constructor() {}

  ngOnInit(): void {}

  // 新增代码
  onRemove() {
    this.remove.emit(this.detail)
  }
}

2.3 pokemon-list.component.html

修改 pokemon-list.component.html, 增加 (remove)="handleRemove($event)":

html 复制代码
<table>
  <thead>
    <th>Name</th>
    <th>Index</th>
  </thead>
  <tbody>
    <app-pokemon-detail
      *ngFor="let pokemon of pokemons"
      [detail]="pokemon"
      (remove)="handleRemove($event)"
    ></app-pokemon-detail>
  </tbody>
</table>

2.4 pokemon-list.component.ts

在此文件中实现删除功能的相应代码:

ts 复制代码
import { Component, OnInit } from '@angular/core';
import { Pokemon } from 'src/app/models/pokemon';

@Component({
  selector: 'app-pokemon-list',
  templateUrl: './pokemon-list.component.html',
  styleUrls: ['./pokemon-list.component.css'],
})
export class PokemonListComponent implements OnInit {
  pokemons: Pokemon[] = [
    // Pokemon: 精灵宝可梦
    {
      id: 1,
      name: 'pikachu', // 皮卡丘
      type: 'electric',
      isCool: false,
      isStylish: true,
    },
    {
      id: 2,
      name: 'squirtle', // 杰尼龟
      type: 'water',
      isCool: true,
      isStylish: true,
    },
    {
      id: 3,
      name: 'charmander', // 小火龙
      type: 'fire',
      isCool: true,
      isStylish: false,
    },
  ];

  constructor() {}
  
  // 新增代码,实现删除功能
  handleRemove(event: Pokemon) {
    // 要避免改变 state,这里不能直接删除 pokemons 数组元素,因此使用 filter
    this.pokemons = this.pokemons.filter((pokemon: Pokemon) => {
      return pokemon.id !== event.id;
    });
  }

  ngOnInit(): void {}
}

运行 ng serve, 点击相应 button,可实现删除功能:


Angular For Beginners

相关推荐
离别又见离别9 分钟前
vue使用js渲染组件案例(公用打印组件动态渲染)及静默打印实现
前端·javascript·vue
wyhwust9 分钟前
学技术找工作经验分享--前端
前端
BD_Marathon13 分钟前
【JavaWeb】JS_BOM编程_window对象的常见属性
前端
IT_陈寒15 分钟前
Redis性能提升50%的7个关键配置:从慢查询优化到内存碎片整理实战指南
前端·人工智能·后端
还不秃顶的计科生19 分钟前
wps“文件路径与可点击的超链接“之间的相互转换
前端
黛色正浓28 分钟前
【React】极客园案例实践-编辑文章模块和项目打包
前端·react.js·前端框架
徐同保29 分钟前
n8n项目编译时取消类型检测,提交代码时取消校验
开发语言·前端·javascript
不会kao代码的小王35 分钟前
openEuler上Docker部署Kafka消息队列实战
前端·云原生·stable diffusion·eureka
Lenyiin35 分钟前
makefile
java·大数据·前端
涡轮蒸鸭猫喵36 分钟前
-------------------UDP协议+TCP协议-------------------------
java·网络·笔记·网络协议·tcp/ip·udp