Javascript 对象的值为exist,但访问时返回的属性未定义
我发现奇怪的事情。我有AuthService,它保存了我的应用程序的身份验证需求,包括身份验证令牌Javascript 对象的值为exist,但访问时返回的属性未定义,javascript,angular,typescript,ionic3,Javascript,Angular,Typescript,Ionic3,我发现奇怪的事情。我有AuthService,它保存了我的应用程序的身份验证需求,包括身份验证令牌 @IonicPage() @Component({ selector: 'page-login', templateUrl: 'login.html', }) export class LoginPage { constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl:ModalController,public auth: AuthService) {
}
ionViewDidLoad() {
console.log(this.auth)
console.log(this.auth.loggedIn)
if(this.auth.loggedIn){
console.log(this.auth);
this.navCtrl.push("TabsPage");
}
}
}
当我打电话时
console.log(this.auth)
它回来了
但是当我打电话的时候
console.log(this.auth.loggedIn)
它返回空值
这是我的auth.service.ts
import { Injectable, NgZone, Component } from '@angular/core';
import { Storage } from '@ionic/storage';
// Import AUTH_CONFIG, Auth0Cordova, and auth0.js
import { AUTH_CONFIG } from './auth.config';
import Auth0Cordova from '@auth0/cordova';
import * as auth0 from 'auth0-js';
@Injectable()
export class AuthService {
Auth0 = new auth0.WebAuth(AUTH_CONFIG);
Client = new Auth0Cordova(AUTH_CONFIG);
accessToken: string;
user: any;
loggedIn: boolean;
loading = true;
constructor(
public zone: NgZone,
private storage: Storage
) {
this.storage.get('profile').then(user => this.user = user);
this.storage.get('access_token').then(token => this.accessToken = token);
this.storage.get('expires_at').then(exp => {
this.loggedIn = Date.now() < JSON.parse(exp);
this.loading = false;
});
}
login() {
this.loading = true;
const options = {
scope: 'openid profile offline_access'
};
// Authorize login request with Auth0: open login page and get auth results
this.Client.authorize(options, (err, authResult) => {
if (err) {
throw err;
}
// Set access token
this.storage.set('access_token', authResult.accessToken);
this.accessToken = authResult.accessToken;
// Set access token expiration
const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
this.storage.set('expires_at', expiresAt);
// Set logged in
this.loading = false;
this.loggedIn = true;
// Fetch user's profile info
this.Auth0.client.userInfo(this.accessToken, (err, profile) => {
if (err) {
throw err;
}
this.storage.set('profile', profile).then(val =>
this.zone.run(() => this.user = profile)
);
});
});
}
logout() {
this.storage.remove('profile');
this.storage.remove('access_token');
this.storage.remove('expires_at');
this.accessToken = null;
this.user = null;
this.loggedIn = false;
}
isLoggedIn() :boolean{
return this.loggedIn;
}
}
从'@angular/core'导入{Injectable,NgZone,Component};
从'@ionic/Storage'导入{Storage};
//导入AUTH_CONFIG、Auth0Cordova和auth0.js
从“./AUTH.CONFIG”导入{AUTH_CONFIG};
从“@auth0/cordova”导入Auth0Cordova;
从“auth0 js”导入*作为auth0;
@可注射()
导出类身份验证服务{
Auth0=newauth0.WebAuth(AUTH\u CONFIG);
客户端=新的Auth0Cordova(AUTH_CONFIG);
accessToken:字符串;
用户:任何;
loggedIn:布尔型;
加载=真;
建造师(
公共区域:NgZone,
专用存储:存储
) {
this.storage.get('profile')。然后(user=>this.user=user);
this.storage.get('access_token')。然后(token=>this.accessToken=token);
this.storage.get('expires\u at')。然后(exp=>{
this.loggedIn=Date.now(){
如果(错误){
犯错误;
}
//设置访问令牌
this.storage.set('access\u token',authResult.accessToken);
this.accessToken=authResult.accessToken;
//设置访问令牌过期时间
const expiresAt=JSON.stringify((authResult.expiresIn*1000)+new Date().getTime());
this.storage.set('expires_at',expiresAt);
//设置已登录
这一点:加载=错误;
this.loggedIn=true;
//获取用户的配置文件信息
this.Auth0.client.userInfo(this.accessToken,(err,profile)=>{
如果(错误){
犯错误;
}
this.storage.set('profile',profile.),然后(val=>
this.zone.run(()=>this.user=profile)
);
});
});
}
注销(){
本文件为.storage.remove('profile');
this.storage.remove('access_token');
this.storage.remove('expires_at');
this.accessToken=null;
this.user=null;
this.loggedIn=false;
}
isLoggedIn():布尔值{
返回这个.loggedIn;
}
}
我正在使用ionic3和auth0身份验证,以前我认为在我的属性上不使用公共标识符是我的错。但是,当我将属性更改为public,或者创建返回属性的getter方法时,它仍然无法工作。这是由于chrome控制台对对象求值的原因。如果你在控制台中打开这个对象,你会看到一个小小的蓝色信息图标。这将说明: 价值刚刚评估过 基本上,在您记录对象内容和在控制台中打开对象内容之间,对象内容发生了变化
登录操作是异步的,这意味着在调用
ionViewDidLoad
之后,将设置auth对象的loggedIn
属性。也许一个好办法是在提供者内部设置auth,或者在auth上有一些可观察的内容,您可以在其上侦听auth更改。在分配之前,您已将this.loggedIn冷却,因此它是未定义的。在您的情况下,在登录后编写console.log(this.auth.loggedIn)。请检查这个场景 2.现在,为loggedIn变量指定一些值,然后打印它将打印一个值 loggedIn:boolean=>loggedIn:boolean=false强> 然后打印一个值,这样就行了 在其他一些组件中
ngOnInit() {
console.log(this.auth.loggedIn)
}
如果我在app_initilzer中设置auth。我还需要observable吗?@rezaridwansyah我想你需要,例如,当auth令牌过期并且用户仍然登录在auth.service.ts中时,我使用isLoggedIn():布尔{return this.loggedIn;}并仍然返回未定义的