我一直在登录页面中使用handleWindowCallback()方法,因为当我登录登录页面时会检查身份验证并重定向到仪表板页面。它工作正常,没有任何缺陷。
但是当我再次尝试使用 acquireToken 检索数据时,应用程序正在重定向到登录页面,这只是第一次发生。这会导致内容呈现延迟。
登录组件.ts
ngOnInit() {
this.webService.handleWindowCallback();
console.log(this.webService.authenticate);
if (this.webService.authenticate) {
console.log('Redirecting to dashboard');
this.router.navigate(['/', 'dashboard']);
}
console.log('login loaded');
}
仪表板组件.ts
ngOnInit() {
this.showLoader = true;
this.webService.getUser();
this.getdata();
console.log('content loaded');
}
getdata() {
const listname = 'Tenets';
const queryParams = '?$filter=Title eq \'Dashboard\'&$select=Title,Description,Small_x0020_Text,Large_x0020_Text';
this.webService.getdata(listname, queryParams).subscribe(data => {
// debugger;
console.log(data);
console.log('content loading');
Object.keys(data['value']).forEach(ele => {
this.dashboardItems.push({
'link': data['value'][ele]['Description'],
'smallText': data['value'][ele]['Small_x0020_Text'],
'largeText': data['value'][ele]['Large_x0020_Text']
});
});
});
}
网络服务.ts
getdata(listname, queryParams): Observable<any> {
const url = environment.config.spUrl + environment.config.spSiteCollection
+ '_api/web/lists/getByTitle(\'' + listname + '\')/items' + queryParams;
return this.adalService.acquireToken(environment.config.spUrl).flatMap(
token => {
const headersParam = new HttpHeaders({
'Content-Type': 'application/json;odata=verbose',
'Authorization': 'Bearer ' + token.toString()
});
return this.http.get(url, { headers: headersParam });
});
}
我需要在 ngOnInit() getdata() 期间检索数据,它应该加载一次,它将呈现内容而无需再次重定向到 loginComponent。
请帮助解决这个问题。它已经吃了我的 2 天。