Angular异步管道减少了流值,而不是显示最新的发射。
创始人
2024-10-30 12:00:15
0

可以在管道中使用RxJS的“tap”操作符来获取最新的值,然后用“startWith”操作符将其初始值发送到管道中。具体代码如下所示:

// 在组件模板中,使用异步管道来显示用户的名称

{{ (user$ | async)?.name }}

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { tap, startWith } from 'rxjs/operators';

interface User {
  id: number;
  name: string;
}

@Component({
  selector: 'app-user',
  template: `...`,
})
export class UserComponent implements OnInit {
  user$: Observable;

  ngOnInit() {
    this.user$ = this.userService.getUser().pipe(
      tap((user) => console.log(user)), // 获取最新的值并打印到控制台
      startWith(null) // 将初始值 null 发送到管道中
    );
  }
}

相关内容

热门资讯

有消息称!雀姬手游透视辅助,禅... 有消息称!雀姬手游透视辅助,禅游游戏辅助(透视)器-一贯真的是有挂1、游戏颠覆性的策略玩法,独创攻略...
wepoker买脚本靠谱吗!w... wepoker买脚本靠谱吗!wepoker有透视功能吗(透视)软件-原来解迷存在有挂wepoker有...
经核实!微信四川熊猫辅助软件,... 经核实!微信四川熊猫辅助软件,川南休闲辅助(透视)软件-真是是有挂微信四川熊猫辅助软件辅助器是一种具...
hhpoker真的有透视吗!w... hhpoker真的有透视吗!wejoker内置辅助(透视)插件-一贯教你有挂1)hhpoker真的有...
受玩家影响!创思维激k透视软件... 受玩家影响!创思维激k透视软件,中至小程序破解(透视)技巧-果然有挂1、每一步都需要思考,不同水平的...
fishpoker透视底牌!p... fishpoker透视底牌!pokemmo脚本辅助器下载(透视)神器-真是科普是有挂1、在pokem...
受玩家影响!微信微乐小程序辅助... 受玩家影响!微信微乐小程序辅助器免费安装,九游破解辅助插件hhpoker(透视)技巧-果然真的有挂微...
wepoker网页版透视方法!... wepoker网页版透视方法!hhpoker破解工具(透视)插件-好像总结真的是有挂1、每一步都需要...
据权威媒体报道!新漫游大厅辅助... 据权威媒体报道!新漫游大厅辅助,阿拉斗牌辅助(透视)app-都是真的有挂1、玩家可以在新漫游大厅辅助...
pokemmo辅助器!hhpo... pokemmo辅助器!hhpoker是真的假的(透视)技巧-一贯分享真的有挂1)hhpoker是真的...