angular - Angular 路由保持显示前一个组件并且不会完全加载下一个
问题描述
我有一个组件,它是一个动态生成的角度表,必须显示在“表”路径中。但首先,该应用程序在“身份验证”页面上启动,这是我的登录页面。如果身份验证正确,我会被重定向到 /table 没问题,但是:1)仍然显示身份验证组件 2)未加载表,未调用 ngOnInit(),似乎没有加载任何 ts 代码,只是基本的html。
如果我禁用canActivate(),它不会改变任何东西,但如果我将routerLink放到auth组件中的/table,一切正常......
这可能与我的身份验证方式(GoogleAuth)有关,所以,这里有一些代码,即使我不确定它是否有用..
auth.component.ts
import { Component, OnInit, ViewChild, ElementRef} from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss']
})
export class AuthComponent implements OnInit {
constructor(private authService: AuthService, private router: Router){}
auth2: any;
@ViewChild('loginRef', {static: true }) loginElement: ElementRef;
ngOnInit() {
this.googleSDK();
}
prepareLoginButton() {
this.auth2.attachClickHandler(this.loginElement.nativeElement, {},
(googleUser) => {
//ici, l'utilisateur est authentifié
let profile = googleUser.getBasicProfile();
console.log('Token || ' + googleUser.getAuthResponse().id_token);
console.log('ID: ' + profile.getId());
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
// YOUR CODE HERE
this.authService.isAuth = true;
this.router.navigate(['/table']);
}, (error) => {
alert(JSON.stringify(error, undefined, 2));
});
}
googleSDK() {
window['googleSDKLoaded'] = () => {
window['gapi'].load('auth2', () => {
this.auth2 = window['gapi'].auth2.init({
client_id: '88179289847-pcj5c8u563k4igvqof1k65tcbqu4q97f.apps.googleusercontent.com',
cookie_policy: 'single_host_origin',
scope: 'profile email'
});
this.prepareLoginButton();
});
}
console.log("google sdk");
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://apis.google.com/js/platform.js?onload=googleSDKLoaded";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'google-jssdk'));
}
}
auth.guard.ts
import { Injectable, OnInit } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService} from '../services/auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
ngOnInit(){
}
constructor (private authService: AuthService, private router: Router){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if(this.authService.isAuth){
return true;
}
else{
this.router.navigate(['/auth']);
}
return true;
}
}
解决方案
你正在走出NgZone
. 的回调this.auth2.attachClickHandler
不再在区域中,因此路由时不再进行更改检测。您应该使用以下方法更新您的代码以重新进入该区域ngZone.run()
:
constructor(
private authService: AuthService,
private router: Router,
private ngZone: NgZone
){}
this.auth2.attachClickHandler(this.loginElement.nativeElement, {}, (googleUser) => {
this.ngZone.run(() => {
// ...
this.router.navigate(['/table']);
});
});
推荐阅读
- javascript - 添加 Autodesk forge 扩展
- ansible - 使用变量的 Ansible 执行
- url-routing - 如何让 Sphinx 解析从 /about-manual 到 /about-manual.html 的 URL 链接
- macos - 是否可以在不打开新窗口的情况下更改 MvvMCross 中的视图?
- css - 如何删除scss中的一些条件值?
- node.js - Handlebars 似乎不允许我访问传递给模板的对象内的数组
- java - Spring Data nativeQuery中的二维数组参数
- android - Android XML Drawable:带有内边缘渐变的圆角矩形
- yii2 - 无法在我的索引页 yii2 中设置部分视图
- python - 如何将多个列表值作为函数参数传递?