我在角度方面有重复性问题,但是我对此问题进行了很多搜索,并使用了所有在stackoverflow和...中都能回答的技术。
当我订阅多个时,我的问题出在加载器组件中。
这是我的加载器组件
import { Component, ChangeDetectionStrategy, ChangeDetectorRef, DoCheck, OnChanges, AfterViewInit, OnInit } from '@angular/core';
import { Subject, BehaviorSubject } from 'rxjs';
import { LoaderService } from './loader.service';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-loader',
templateUrl: './loader.component.html',
styleUrls: ['./loader.component.scss']
})
export class LoaderComponent implements OnInit {
isLoading: BehaviorSubject<boolean>=this.loaderService.isLoading;
constructor(private loaderService: LoaderService, private changeDetector: ChangeDetectorRef) {
}
color = 'accent';
mode = 'indeterminate';
value = 50;
ngOnInit(): void {
}
}
这是我的服务加载程序组件
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LoaderService {
constructor() { }
isLoading: BehaviorSubject<boolean> = new BehaviorSubject(false);
count=0;
show(): void {
debugger
console.log(`show`+this.count++)
this.isLoading.next(true);
}
hide(): void {
debugger
console.log(`hide`+this.count++)
this.isLoading.next(false);
}
}
这是我的拦截装载机
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { LoaderService } from './loader/loader.service';
@Injectable()
export class LoaderInterceptor implements HttpInterceptor {
constructor(public loaderService: LoaderService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.loaderService.show();
return next.handle(req).pipe(
finalize(() => {this.loaderService.hide(); })
);
}
}
我的错误消息是 “
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: [object Object]'. Current value: 'ngIf: true'.
at viewDebugError (core.js:17871)
at expressionChangedAfterItHasBeenCheckedError (core.js:17859)
at checkBindingNoChanges (core.js:18059)
at checkNoChangesNodeInline (core.js:27635)
at checkNoChangesNode (core.js:27624)
at debugCheckNoChangesNode (core.js:28228)
at debugCheckDirectivesFn (core.js:28156)
at Object.updateDirectives (loader.component.html:2)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:28145)
at checkNoChangesView ("
请帮我解决。这是我的大问题:-(
答案 0 :(得分:0)
我不确定使用ngIf语句的位置,但是另一种选择是在不使用时使用CSS隐藏加载程序。例如
<div #myLoader [style.display]="isLoading ? 'block' : 'none'>...
答案 1 :(得分:0)
为避免这种情况,请为isLoading属性设置默认值(例如,false),然后等待ngOnInit或ngAfterViewInit更改组件中的属性。
答案 2 :(得分:0)
试图将代码复制到具有loaderService
多个订阅的独立stackblitz实例https://stackblitz.com/edit/angular-multisub中。
工作正常。
您能分叉上述实例并进行修改以重现吗?
答案 3 :(得分:0)
我正在将“行为主题”更改为可观察的。在加载页面中订阅数据并在生命周期中使用了角度变化检测器。现在,问题已解决并正常工作