[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

相关推荐
m_Molly8 分钟前
C++源代码封装成dll动态链接库,并在WPF项目中使用的步骤说明
笔记
点燃银河尽头的篝火(●'◡'●)21 分钟前
【BurpSuite】Cross-site scripting (XSS 学徒部分:1-9)
前端·web安全·网络安全·xss
Jiaberrr1 小时前
手把手教你:微信小程序实现语音留言功能
前端·微信小程序·小程序·语音·录音
熊猫在哪1 小时前
安装nuxt3
前端·nuxt.js
安冬的码畜日常1 小时前
【CSS in Depth 2 精译_036】5.6 Grid 网格布局中与对齐相关的属性 + 5.7本章小结
前端·css·css3·html5·网格布局·grid·css网格
完球了1 小时前
【Day03-MySQL单表】
数据库·笔记·学习·mysql·sql8.0
酒饮微醉-2 小时前
论文阅读--Planning-oriented Autonomous Driving(二)
论文阅读·笔记·学习·自动驾驶
你可以叫我仔哥呀2 小时前
学习笔记JVM篇(五)
jvm·笔记·学习
码农0002 小时前
nginx学习笔记
笔记·学习·nginx
啧不应该啊2 小时前
vue配置axios
前端·javascript·vue.js