当前位置: 首页 > news >正文

[Angular] 笔记 11:可观察对象(Observable)

chatgpt:

在 Angular 中,Observables 是用于处理异步数据流的重要工具。它们被广泛用于处理从异步操作中获取的数据,比如通过 HTTP 请求获取数据、定时器、用户输入等。Observables 提供了一种机制来订阅这些数据流,并可以在数据到达时执行相应的操作。其优势在于能够处理异步操作、多个事件以及数据的转换和组合,使得对数据流的处理更为灵活和高效。在 Angular 中,Observables 常用于处理 HTTP 请求、与路由、表单和事件处理等方面。


当涉及 Observable,20%的知识覆盖了80%的应用场景。

Observable在代码中被广泛应用于异步操作。

异步操作在前端确保 UI 界面不会冻结,在后端能够提高系统性能。

在这里插入图片描述
Observer 和 Observable 之间的关系:

在这里插入图片描述

接下来安装一个用于测试的后端: json-server.

vscode terminal 运行: npm i -g json-server,这里必须有 -g,否则后面的 json-server 命令会无法识别。
然后在项目的根目录创建文件 db.json

在这里插入图片描述

db.json 文件内容:

{"pokemon": [{"id": 1,"name": "pikachu","type": "electric","isCool": false,"isStylish": true},{"id": 2,"name": "squirtle","isCool": true,"isStylish": true},{"id": 3,"name": "charmander","type": "fire","isCool": true,"isStylish": false}]
}

vscode terminal 运行命令:

json-server --watch db.json

PS D:\Angular\my-app> json-server --watch db.json\{^_^}/ hi!Loading db.jsonDoneResourceshttp://localhost:3000/pokemonHomehttp://localhost:3000

访问 http://localhost:3000/pokemon,可以看到用于测试的数据:

在这里插入图片描述
pokeman-base.module.ts 文件中 import HttpClientModule:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PokemonListComponent } from './pokemon-list/pokemon-list.component';
import { PokemonDetailComponent } from './pokemon-detail/pokemon-detail.component';
import { PokemonService } from '../services/pokemon.service';
import { HttpClientModule } from '@angular/common/http';@NgModule({declarations: [PokemonListComponent, PokemonDetailComponent],imports: [CommonModule, HttpClientModule],  // Add HttpClientModule!!!exports: [PokemonListComponent, PokemonDetailComponent],providers: [PokemonService],
})
export class PokemonBaseModule {}

接下来访问 http client, pokemon.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; // 新增 import
import { Pokemon } from '../models/pokemon';
import { Observable } from 'rxjs';// endpoint
const POKEMON_API = 'http://localhost:3000/pokemon';@Injectable({providedIn: 'root',
})
export class PokemonService {constructor(private http: HttpClient) {}getPokemons(): Observable<Pokemon[]> {return this.http.get<Pokemon[]>(POKEMON_API);}
}

pokeman-list.component.ts:

import { Component, OnInit } from '@angular/core';
import { Pokemon } from 'src/app/models/pokemon';
import { PokemonService } from 'src/app/services/pokemon.service';@Component({selector: 'app-pokemon-list',templateUrl: './pokemon-list.component.html',styleUrls: ['./pokemon-list.component.css'],
})
export class PokemonListComponent implements OnInit {pokemons!: Pokemon[];// 修改 constructorconstructor(private pokemonService: PokemonService) {}handleRemove(event: Pokemon) {this.pokemons = this.pokemons.filter((pokemon: Pokemon) => {return pokemon.id !== event.id;});}ngOnInit(): void {// 填充 pokemons 属性// this.pokemons = this.pokemonService.getPokemons();this.pokemonService.getPokemons().subscribe((data: Pokemon[])=>{console.log(data)this.pokemons = data;})}
}

运行 ng serve:

在这里插入图片描述


Angular for Beginners

http://www.lryc.cn/news/268866.html

相关文章:

  • 【论文阅读】Resource Allocation for Text Semantic Communications
  • VMware16 pro 安装openEuler-23.09-x86_64,详细操作流程+详图。
  • Mybatis 动态 SQL - script,bind,多数据库支持
  • Scikit-Learn线性回归(一)
  • Mybatis 动态 SQL - choose, when, otherwise
  • idea Spring Boot项目使用JPA创建与数据库链接
  • redis基础知识
  • 最短路径(数据结构实训)(难度系数100)
  • 基于SSM实现的电动汽车充电网点管理系统
  • Android ImageView如何使用.svg格式图片
  • 力扣热题100道-子串篇
  • day3--Shell
  • 【数据结构】插入排序、选择排序、冒泡排序、希尔排序、堆排序
  • TiDB 7.5 LTS 发版丨提升规模化场景下关键应用的稳定性和成本的灵活性
  • 服务器数据恢复-误操作导致xfs分区数据丢失的数据恢复案例
  • 安装Kubernetes1.23、kubesphere3.4、若依项目自动打包部署到K8S记录
  • (三) `MaterializedMySQL`同步机制解读
  • 使用 stream 流构建树(不使用递归)
  • docker 部署 个人网页版 wps office
  • windows进行udp端口转发,解决项目中服务器收不到组播数据的问题
  • 抖音、小红书、视频号是如何判定是否限流的?
  • frida native hook 技术( frida hook so层函数)
  • SpringBoot运维(三)-- 多环境开发(yml多文件版)
  • Vue 修饰符有哪些
  • 哈希桶的模拟实现【C++】
  • 磁盘相关知识
  • FTP原理与配置
  • ios环境搭建_xcode安装及运行源码
  • C++ 151. 反转字符串中的单词
  • 腾讯云服务器如何买(购买腾讯云服务器的详细步骤)