mat-progress-bar 问题,BehaviorSubject 不工作
mat-progress-bar issue, BehaviorSubject not working
我不知道是什么问题:(
问题是isLoadingLogin总是设置为false,所以mat进度条不显示。
我有以下 LoginFormComponent:
template: `
{{(isLoadingLogin | async) | json }}
<mat-progress-bar *ngIf="(isLoadingLogin | async)" mode="indeterminate" class="progress-bar"></mat-progress-bar>
`,
styleUrls: ['./login-form.component.scss']
})
export class LoginFormComponent implements OnInit, OnDestroy {
isLoadingLogin: Observable<boolean>;
ngOnInit() {
this.isLoadingLogin = this.userService.isLoading;
}
login() {
if (this.form.valid) {
const email = this.form.get('email').value;
const password = this.form.get('password').value;
this.sessionService.login(email, password);
}
}
}
以及以下服务
@Injectable({
providedIn: 'root'
})
export class SessionService {
login(email: string, password: string) {
this.authService.login(email, password).subscribe(token => {
this.loginUser(token);
});
}
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
login(email: string, password: string) {
return this.userService.login(email, password).pipe(map(({token}: any) => this.storeToken(token)));
}
}
@Injectable()
export class UserService {
private isLoadingLogin: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
isLoading: Observable<boolean> = this.isLoadingLogin.asObservable();
login(user, password) {
this.isLoadingLogin.next(true);
console.log(this.isLoadingLogin.value);
return this.http.post(this.loginUrl, JSON.stringify({
user,
password
}), this.httpOptions)
.pipe(
catchError(error => this.errorHandleService.handleError('login', error)),
finalize(() => {
this.isLoadingLogin.next(false);
console.log(this.isLoadingLogin.value);
})
);
}
}
P.S。我检查了 UserService 是否已在@NgModule 上正确导入。
已解决:
@Injectable({ providedIn: 'root' })
export class UserService { }
并将其从我的提供商模块中删除。
我不知道是什么问题:(
问题是isLoadingLogin总是设置为false,所以mat进度条不显示。
我有以下 LoginFormComponent:
template: `
{{(isLoadingLogin | async) | json }}
<mat-progress-bar *ngIf="(isLoadingLogin | async)" mode="indeterminate" class="progress-bar"></mat-progress-bar>
`,
styleUrls: ['./login-form.component.scss']
})
export class LoginFormComponent implements OnInit, OnDestroy {
isLoadingLogin: Observable<boolean>;
ngOnInit() {
this.isLoadingLogin = this.userService.isLoading;
}
login() {
if (this.form.valid) {
const email = this.form.get('email').value;
const password = this.form.get('password').value;
this.sessionService.login(email, password);
}
}
}
以及以下服务
@Injectable({
providedIn: 'root'
})
export class SessionService {
login(email: string, password: string) {
this.authService.login(email, password).subscribe(token => {
this.loginUser(token);
});
}
}
@Injectable({
providedIn: 'root'
})
export class AuthService {
login(email: string, password: string) {
return this.userService.login(email, password).pipe(map(({token}: any) => this.storeToken(token)));
}
}
@Injectable()
export class UserService {
private isLoadingLogin: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
isLoading: Observable<boolean> = this.isLoadingLogin.asObservable();
login(user, password) {
this.isLoadingLogin.next(true);
console.log(this.isLoadingLogin.value);
return this.http.post(this.loginUrl, JSON.stringify({
user,
password
}), this.httpOptions)
.pipe(
catchError(error => this.errorHandleService.handleError('login', error)),
finalize(() => {
this.isLoadingLogin.next(false);
console.log(this.isLoadingLogin.value);
})
);
}
}
P.S。我检查了 UserService 是否已在@NgModule 上正确导入。
已解决:
@Injectable({ providedIn: 'root' })
export class UserService { }
并将其从我的提供商模块中删除。