Typescript angular2路由器。在auth0回调中导航
调用路由器后呈现模板时遇到问题。请在auth0lock的回调中导航 loginComponent.tsTypescript angular2路由器。在auth0回调中导航,typescript,angular,angular2-routing,auth0,Typescript,Angular,Angular2 Routing,Auth0,调用路由器后呈现模板时遇到问题。请在auth0lock的回调中导航 loginComponent.ts import {Component, Inject} from 'angular2/core'; import {Router, ComponentInstruction} from 'angular2/router'; import {Auth} from '../auth'; declare var Auth0Lock; @Component({ selector: 'log
import {Component, Inject} from 'angular2/core';
import {Router, ComponentInstruction} from 'angular2/router';
import {Auth} from '../auth';
declare var Auth0Lock;
@Component({
selector: 'login',
templateUrl: '/tpls/login/login.html'
})
export class LoginComponent {
private lock = new Auth0Lock('xxx', 'xxx.auth0.com');
constructor(@Inject(Router) private router: Router, @Inject(Auth) private auth: Auth) { }
logError = (err) => {
console.log(err);
}
loginSuccess = (data) => {
if(this.router.parent.lastNavigationAttempt !== undefined && this.router.parent.lastNavigationAttempt !== '/Login') {
this.router.navigateByUrl(this.router.parent.lastNavigationAttempt);
} else if(data.user.req_update) {
this.router.navigate(['Profile']);
} else {
this.router.navigate(['Home']);
}
}
ngOnInit() {
this.lock.show((err: Error, profile: any, id_token: string) => {
if(err) return this.logError(err);
return this.auth.login(profile, id_token);
});
this.auth.loginSuccess.subscribe(
data => this.loginSuccess(data),
err => this.logError(err)
);
}
}
import {Component, Inject, OnInit} from 'angular2/core';
import {Http} from 'angular2/http';
import {ROUTER_DIRECTIVES} from 'angular2/router'
import {Auth} from '../auth';
import {Post} from '../post/Post';
import {IPost} from '../post/IPost';
import {AuthorComponent} from '../author/authorComponent';
@Component({
selector: 'home',
templateUrl: '/tpls/home/home.html',
directives: [ROUTER_DIRECTIVES, AuthorComponent]
})
export class HomeComponent implements OnInit {
private postService: Post;
private posts: IPost[];
constructor(@Inject(Http) private http: Http, @Inject(Auth) private auth: Auth) {
console.log('constructor');
this.postService = new Post(this.http, this.auth);
this.getPosts();
}
getPosts = () => {
this.postService.all().subscribe(
data => this.getPostsCallback(data.json()),
err => this.logError(err)
);
}
getPostsCallback = (data) => {
console.log('callback');
this.posts = data;
}
logError = (err) => {
console.log(err);
}
ngOnInit() {
console.log('init');
//this.postService = new Post(this.http, this.auth);
//this.getPosts();
}
}
auth.ts
import {Injectable, Inject, EventEmitter, Output } from 'angular2/core';
import {Http, Headers} from 'angular2/http';
@Injectable()
export class Auth {
...
@Output() loginSuccess = new EventEmitter();
login = (profile, id_token) => {
...
this.addUser(profile).subscribe(
data => {
this.loginSuccess.next(data.json());
},
err => {
console.log(err);
this.loginSuccess.error(err.json());
}
);
}
addUser = (user: any) => {
let body = JSON.stringify(user);
return this.http.post('/api/user/add', body, { headers: this.headers});
}
}
homeComponent.ts
import {Component, Inject} from 'angular2/core';
import {Router, ComponentInstruction} from 'angular2/router';
import {Auth} from '../auth';
declare var Auth0Lock;
@Component({
selector: 'login',
templateUrl: '/tpls/login/login.html'
})
export class LoginComponent {
private lock = new Auth0Lock('xxx', 'xxx.auth0.com');
constructor(@Inject(Router) private router: Router, @Inject(Auth) private auth: Auth) { }
logError = (err) => {
console.log(err);
}
loginSuccess = (data) => {
if(this.router.parent.lastNavigationAttempt !== undefined && this.router.parent.lastNavigationAttempt !== '/Login') {
this.router.navigateByUrl(this.router.parent.lastNavigationAttempt);
} else if(data.user.req_update) {
this.router.navigate(['Profile']);
} else {
this.router.navigate(['Home']);
}
}
ngOnInit() {
this.lock.show((err: Error, profile: any, id_token: string) => {
if(err) return this.logError(err);
return this.auth.login(profile, id_token);
});
this.auth.loginSuccess.subscribe(
data => this.loginSuccess(data),
err => this.logError(err)
);
}
}
import {Component, Inject, OnInit} from 'angular2/core';
import {Http} from 'angular2/http';
import {ROUTER_DIRECTIVES} from 'angular2/router'
import {Auth} from '../auth';
import {Post} from '../post/Post';
import {IPost} from '../post/IPost';
import {AuthorComponent} from '../author/authorComponent';
@Component({
selector: 'home',
templateUrl: '/tpls/home/home.html',
directives: [ROUTER_DIRECTIVES, AuthorComponent]
})
export class HomeComponent implements OnInit {
private postService: Post;
private posts: IPost[];
constructor(@Inject(Http) private http: Http, @Inject(Auth) private auth: Auth) {
console.log('constructor');
this.postService = new Post(this.http, this.auth);
this.getPosts();
}
getPosts = () => {
this.postService.all().subscribe(
data => this.getPostsCallback(data.json()),
err => this.logError(err)
);
}
getPostsCallback = (data) => {
console.log('callback');
this.posts = data;
}
logError = (err) => {
console.log(err);
}
ngOnInit() {
console.log('init');
//this.postService = new Post(this.http, this.auth);
//this.getPosts();
}
}
我正在索引页中包含authlock的cdn脚本。似乎登录后导航到的任何路径都不会呈现。来自this.lock.show的回调有效,我可以读取变量。非常感谢您的建议
基本概念:这应该可以解决您的问题
import { Component, NgZone } from '@angular/core';
constructor(public router: Router, public _zone: NgZone) {}
然后在你的回调中,调用这个
this._zone.run(()=>{
this.router.navigate(['uploader']);
});
多亏了我找到了问题的解决方案,因为NgZone
不适合我
将请求包装在bindNodeCallback
中就成功了。
此函数使用Zone.js包装Auth0请求,从而在角度区域中执行请求
我的代码是:
this._auth0.parseHash((err, authResult: auth0.Auth0DecodedHash) => {
if (!err && authResult && authResult.accessToken && authResult.idToken) {
window.location.hash = ''; // Remove the Auth0 trailing hash
}
this.ngZone.run(() => {
this.router.navigate(['<some-url>']);
});
});
this.\u auth0.parseHash((错误,authResult:auth0.Auth0DecodedHash)=>{
if(!err&&authResult&&authResult.accessToken&&authResult.idToken){
window.location.hash='';//删除Auth0尾部散列
}
此.ngZone.run(()=>{
这个.router.navigate(['']);
});
});
但由于超出了角度范围,导致无法导航到任何页面。我用Zone.js包装的版本替换了该代码以使其正常工作:
this.parseHash$ = bindNodeCallback(this._auth0.parseHash.bind(this._auth0));
this.parseHash$().subscribe(() => this.router.navigate(['<some-url>']));
this.parseHash$=bindNodeCallback(this.\u auth0.parseHash.bind(this.\u auth0));
this.parseHash$().subscribe(()=>this.router.navigate(['']);
可以在subscribe
或通过map
功能编辑请求结果
版本 对于上述代码,我使用以下版本: 角度
6.1.7
auth0 js
9.8.0
我有两个在任何异步类型代码中使用router.navigate和router.navigateByUrl时出现问题。如果对路由器的调用是承诺、回调或可观察的,那么它似乎总是失败。我尝试过使用NgZone等方法,但我唯一能够始终如一地工作的是将导航放在任何异步代码之外。我很想找到一个解决方案。这似乎真的是一个模糊的解决方案。我看不出对ngZone.run的调用如何“超出范围”,如果它实际上可以访问这个this.router属性。嘿,我遇到了完全相同的问题。。。试图遵循您最终的解决方案,但我不清楚回调代码的去向。我可以确认NgZone解决方案不起作用。您可以发布解决方案的完整代码吗?您好@user1543276,我不再从事该项目,因此无法发布确切的代码,但您可以在这里找到我遵循的示例代码: