我有一個站點,我想在將憑據發送到 API(端點)的場景中以編程方式登錄。因此,如果我手動登錄,該應用程式運行良好。
現在我有一個條件,如果是真的,就登錄一下,這段代碼在app.component.ts
export class AppComponent {
{
this.initializeApp();
}
initializeApp() {
if((this.platform.is('mobileweb') || this.platform.is('desktop')) && this.id == "C3E1EE21-A62C-452F-BE0D-AC3EF5449F23"){
//Authenticate
var loginModel: myLoginModel =
{
applicationId: "C1F3A7D7-709D-EB11-A008-00155E00F00B",
username: "username",
password: "pwrd5893"
};
this._auth.login(loginModel).then(token => {
let user:any = jwt_decode(token);
user.token = token;
this.storageService.setItem('user',user);
}).catch(err => {
console.log(err);
});
this.navController.navigateRoot('myScreen');
}
}
}
_auth.login 方法在 auth.service.ts 上:
login(credentials): Promise<any> {
return this._api.security_post('Login', credentials)
.then((res: any) => {
this.storageService.setItem('token',res);
return res;
}).catch(e => {
this.logout();
return e;
})
}
而 this._api.security_post 在 auth.login 上使用它在 api.service.ts
security_post(url, data) {
const promise = new Promise((resolve, reject) => {
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
responseType: 'text' as 'json'
};
this.http.post(this.apiSecurity '/' url, data, httpOptions).subscribe(data => {
resolve(data)
},error=>{
reject(error);
});
})
return promise;
}
所以應用程式登錄正確,但它一直在做無限的登錄請求

我該如何解決?問候
uj5u.com熱心網友回復:
我猜你想以編程方式登錄的條件是this.id == "C3E1EE21-..."?
在這種情況下,我想它會true在您的 App 會話中保留,因此在每次組件更新時都會執行該塊。
一個可能的解決方案可能是添加一個額外的條件,通常僅當您的會話中當前沒有已知用戶時才登錄:
if((
this.platform.is('mobileweb')
|| this.platform.is('desktop')
) && this.id == "C3E1EE21-..."
&& !this.storageService.getItem('user') ) {
//Authenticate...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414488.html
標籤:
