在视图中遇到问题,无法识别组件的价值变化
我尝试使用ChangeDetectorRef
,但仍然没有任何结果。尝试了一堆东西,我花了很多时间在我认为应该可以工作的东西上。我感觉到gapi
处理onsucess
函数的方式与它有关,但我也尝试将其绑定下来,将其包装在其他函数中,似乎没有任何作用。
onsuccess
几乎立即运行,但是即使我注销并重新登录我也得到相同的结果。
我在这里错过了什么吗?
header.component.html<mat-toolbar color="">
<span routerLink="">Hub</span>
<span class="spacer"></span>
<ul >
<li [hidden]="!userIsAuthenticated" >
<a mat-button (click)="onLogout()" routerLinkActive="mat-accent">LogOut</a>
</li>
<li [hidden]="userIsAuthenticated">
<app-google-signin></app-google-signin>
</li>
</ul>
</mat-toolbar>
header.component.ts
import {AfterViewInit, ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {AuthService} from '../auth/auth.service';
import {Subscription} from 'rxjs';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit, OnDestroy {
private userIsAuthenticated = false;
private loggingIn = false;
private authStatusListenerSub: Subscription;
private loggingInSub: Subscription;
constructor(private authService: AuthService, private ref: ChangeDetectorRef) {
}
ngOnInit() {
this.userIsAuthenticated = this.authService.getIsAuthenticated();
this.loggingInSub = this.authService.getLoggingInListener().subscribe(loggingIn => {
this.loggingIn = loggingIn
console.log(`Logging In: ${loggingIn}`)
this.ref.markForCheck()
})
this.authStatusListenerSub = this.authService.getAuthStatusListener().subscribe(isAuthenticated=>{
this.userIsAuthenticated = isAuthenticated
console.log(`Authenticated: ${isAuthenticated}`)
});
}
ngOnDestroy() {
this.authStatusListenerSub.unsubscribe();
}
}
验证服务
import {EventEmitter, Injectable, Output} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Subject} from 'rxjs';
declare const gapi: any;
@Injectable({
providedIn: 'root'
})
export class AuthService {
public CLIENT_ID = '[clientId]';
private isAuthenticated = false;
private loggingIn = false;
@Output() authStatus = new EventEmitter<boolean>()
private authStatusListener = new Subject<boolean>();
private loggingInListener = new Subject<boolean>();
constructor(private http: HttpClient) {
}
onGoogleSignIn(googleUser) {
this.loggingIn = true;
this.loggingInListener.next(true)
const googleToken = googleUser.getAuthResponse().id_token;
this.http.post<{ message: string, access: boolean }>(
'[login url]',
{loginToken: googleToken},
{withCredentials: true})
.subscribe(
res => {
console.log(res.message);
this.loggingIn = false;
this.loggingInListener.next(false)
this.isAuthenticated = true;
this.authStatusListener.next(true);
},
(err) => {
this.loggingIn = false;
this.loggingInListener.next(false)
this.logout();
});
}
getIsAuthenticated() {
return this.isAuthenticated;
}
getAuthStatusListener() {
return this.authStatusListener.asObservable();
}
getLoggingInListener() {
return this.loggingInListener.asObservable()
}
}
google-signin.component.ts
import {AfterViewInit, Component, ElementRef, OnInit} from '@angular/core';
import {AuthService} from '../auth.service';
declare const gapi: any;
@Component({
selector: 'app-google-signin',
templateUrl: './google-signin.component.html',
styleUrls: ['./google-signin.component.scss']
})
export class GoogleSigninComponent implements OnInit, AfterViewInit {
public auth2: any;
constructor(
private element: ElementRef,
private authService: AuthService,
) {
}
ngOnInit() {
}
ngAfterViewInit() {
this.googleInit();
}
public googleInit() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: this.authService.CLIENT_ID,
cookie_policy: 'single_host_origin',
});
const element = gapi.signin2.render('app-google-signin', {
scope: 'profile email',
longtitle: true,
theme: 'dark',
onsuccess: this.authService.onGoogleSignIn.bind(this.authService),
onfailure: err => console.log(err)
});
});
}
}
答案 0 :(得分:0)
尝试手动触发变更检测并使用onPush变更检测策略
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush // add this
})
export class HeaderComponent implements OnInit, OnDestroy {
private userIsAuthenticated = false;
private loggingIn = false;
private authStatusListenerSub: Subscription;
private loggingInSub: Subscription;
constructor(private authService: AuthService, private cdr: ChangeDetectorRef) {
}
ngOnInit() {
this.userIsAuthenticated = this.authService.getIsAuthenticated();
this.loggingInSub = this.authService.getLoggingInListener().subscribe(loggingIn => {
this.loggingIn = loggingIn
console.log(`Logging In: ${loggingIn}`)
this.cdr.detectChanges(); // *trigger change here*
})
this.authStatusListenerSub = this.authService.getAuthStatusListener().subscribe(isAuthenticated=>{
this.userIsAuthenticated = isAuthenticated
console.log(`Authenticated: ${isAuthenticated}`)
});
}
ngOnDestroy() {
this.authStatusListenerSub.unsubscribe();
}
}