Angualar16中的effect()函数是否可以像Vue.js的watch一样访问旧值?
创始人
2024-10-14 10:00:42
0

在 Angular 16 中,可以使用rxjs的pairwise操作符来访问旧值。

以下是一个简单的示例:

import { Component, OnInit, OnDestroy } from '@angular/core'; import { pairwise, takeUntil } from 'rxjs/operators'; import { Subject } from 'rxjs';

@Component({ selector: 'app-example', template: Current value is: {{ value }}
Previous value is: {{ oldValue }}
}) export class ExampleComponent implements OnInit, OnDestroy { value: string; oldValue: string; destroyed$: Subject = new Subject();

constructor() { }

ngOnInit() { this.getData().pipe( pairwise(), takeUntil(this.destroyed$) ).subscribe(([oldValue, currentValue]) => { this.oldValue = oldValue; this.value = currentValue; }); }

ngOnDestroy() { this.destroyed$.next(); this.destroyed$.complete(); }

getData() { // return an observable which emits values } }

以上代码中,通过使用rxjs的pairwise操作符来将前一个和当前值作为一个元素传递给观察者。稍后在订阅器中,我们可以使用解构语法将这两个元素分离,并将它们分别存储在 oldValue 和 value 变量中。

另外,这里还使用了takeUntil操作符,以确保在销毁组件前取消Observable的订阅。

相关内容

热门资讯

无独有偶"wepok... 无独有偶"wepoker辅助器安装包定制"aapoker透视脚本(真是是真的辅助平台)-哔哩哔哩we...
透视软件"wepok... 您好,wepoker如何设置透视这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
于此同时"aapok... 于此同时"aapoker怎么设置抽水"wpk辅助(一直真的是有辅助工具)-哔哩哔哩aapoker怎么...
透视透视"werpl... 透视透视"werplan脚本"hhpoker哪个俱乐部靠谱(果然真的有辅助插件)-哔哩哔哩运hhpo...
透视好牌"wepok... 透视好牌"wepoker透视有没有"wepoker私人局开挂视频(好像是有辅助平台)-哔哩哔哩1、w...
围绕透视问题"哈糖大... 围绕透视问题"哈糖大菠萝破解器"aapoker辅助器是真的吗(本来真的有辅助下载)-哔哩哔哩进入游戏...
今天下午"wepok... 您好,德州辅助工具到底怎么样这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
日前"wepoker... 日前"wepoker脚本下载"wepoker私局代打(竟然真的有辅助辅助器)-哔哩哔哩1、每一步都需...
据统计"pokern... 您好,we-poker有人玩吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视讲解"wpk作必... 透视讲解"wpk作必弊是真的吗"wepoker透视app下载(真是真的是有辅助平台)-哔哩哔哩1、完...