angular - *ngFor 行为主题 | 异步不更新
问题描述
我对 BehaviorSubject 和异步管道有疑问。它没有向我显示 .next() 之后的所有 BehaviorSubject 项目。它只显示“prova”项目。我在拦截器中记录了错误$并且我还有其他项目。为什么它不显示页面中的所有项目?谢谢
模板
<div class="alerts">
<div class="alert" *ngFor="let error of (errorInterceptor.errors$ | async) ">
{{error}}
</div>
</div>
错误拦截器
@Injectable({ providedIn: 'root' })
export class ErrorInterceptor implements HttpInterceptor {
errors$ = new BehaviorSubject<string[]>(['prova']);
constructor() {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status >= 400 && err.status < 500) {
console.error('error-interceptor:error status=' + err.status + ' message=' + err.message + ' msg='+err.msg);
const errors = [...this.errors$.value];
errors.push(err.status);
this.errors$.next(errors);
console.table(this.errors$.value);
} else {
console.error('error-interceptor:err=' + err.error + ' statusText=' + err.statusText + ' status:' + err.status);
}
const error = err.error.msg || err.statusText;
return throwError(error);
}));
}
}
解决方案
对,因为值就是你传递给.next
. 如果您想收集所有错误,则需要将代码更改为类似
errors$.next([...errors$.value, newError]);
一个可能的解决方案是创建一个令牌并共享BehaviourSubject
.
providers: [
{
provide: 'errors',
useValue: new BehaviorSubject(['prova']),
},
],
然后在您的组件代码中而不是注入ErrorInterceptor
inject @Inject('errors') public readonly errors$: Observable<string[]>
。
并更新拦截器以执行相同的操作。
constructor(@Inject('errors') protected readonly errors$: BehaviorSubject<string[]>) {}
// ....
this.errors$.next(errors);
之后在模板中你可以使用
<div class="alerts">
<div class="alert" *ngFor="let error of (errors$ | async) ">
{{error}}
</div>
</div>
推荐阅读
- python - 如何创建允许同时回答基于 Django 的调查的唯一/多个会话?
- c# - 无法在单独的方法c#中访问类方法,名称item1在当前上下文中不存在
- c# - Linux 中的 .NET Core 控制台调整大小事件
- php - 如何将单个 CSV 行上传到 PHP 中的不同表中?
- conda - 如何使 conda python 包使用本地 c 库?
- angular - 如何修复 ASP.NET Core Web 应用程序中使用的 Angular 组件中的路由?
- javascript - 脚本从 Live Server 加载,但在打开 index.html 时不加载
- node.js - 无法从 MongoDB 数据库中获取任何项目
- java - Java - 生成每个4个字符排列的列表
- c# - 可以使用 MVVM 在 ViewModel 属性中调用服务吗?