[英]Rxjs/Typescript: ExpressionChangedAfterItHasBeenCheckedError
I'm using ng-select custom server-side search to load data, whether the user provided a search term or not.我正在使用ng-select自定义服务器端搜索来加载数据,无论用户是否提供了搜索词。
component.html组件.html
<ng-select [items]="filterValues$ | async"
[typeahead]="filterValuesInput$"
[multiple]="true"
(open)="getFilterValues(pref.id)"
[loading]="filterValuesLoading"
bindLabel="name"
[(ngModel)]="filter_values">
</ng-select>
component.ts组件.ts
getFilterValues(filterName) {
this.filterValues$ = concat(
of([]), // default items
this.filterValuesInput$.pipe(
startWith([]),
distinctUntilChanged(),
tap(() => this.filterValuesLoading = true),
switchMap(term => this.preferencesService.getFilterValues(filterName, '' + term).pipe(
map(res => res.filter_values),
catchError(() => of([])), // empty list on error
tap(() => this.filterValuesLoading = false)
))
)
);
}
The problem I noticed is that whenever I open the select dropdown, it triggers a console error:我注意到的问题是,每当我打开 select 下拉菜单时,它都会触发控制台错误:
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'loading: false'. Current value: 'loading: true'.
After that the loading spinner triggered by this.filterValuesLoading works fine if a search term is provided.之后,如果提供了搜索词,则由 this.filterValuesLoading 触发的加载微调器可以正常工作。 What is the problem here?
这里有什么问题? Thanks!
谢谢!
It is happening because you are changing the value of filterValues$
on open, which shouldn't be the case, you need to define filterValues$
once, you don't need to do it on each open, I think you can get rid off that function, and initiate your filterValues$
once.发生这种情况是因为您在打开时更改了
filterValues$
的值,这不应该是这种情况,您需要定义一次filterValues$
,您不需要在每次打开时都这样做,我认为您可以摆脱function,并启动您的filterValues$
一次。 Ideally I would define a property filterValues$
and assign the value once only, during init example here , check the loadPeople
function, called once, during on init, and they don't use open event emitter at all.理想情况下,我会定义一个属性
filterValues$
并只分配一次值,在这里的初始化示例中,检查loadPeople
function,在初始化期间调用一次,它们根本不使用打开事件发射器。
I managed to get rid of the error.我设法摆脱了错误。
this.filterValuesLoading
and this.filterValuesInput$
were initialized using ngOnInit lifecycle hook, however using tap(() => this.filterValuesLoading = true)
caused the error I described in the first post. this.filterValuesLoading
和this.filterValuesInput$
是使用 ngOnInit 生命周期钩子初始化的,但是使用tap(() => this.filterValuesLoading = true)
导致了我在第一篇文章中描述的错误。 I removed that line and put this.filterValuesLoading = true
at the beginning of getFilterValues()
method.我删除了该行并将
this.filterValuesLoading = true
放在getFilterValues()
方法的开头。
I'm open for other suggestions since I don't think this is the best approach我愿意接受其他建议,因为我认为这不是最好的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.