import { NativeStorage } from '@ionic-native/native-storage/ngx';
export class AuthService {
userData: any; // Save logged in user data
constructor(
public nativeStorage: NativeStorage,
private platform :Platform,
public ngZone: NgZone // NgZone service to remove outside scope warning
) { }
// Sign in with email/password
// Returns true when user is logged in and email is verified
get isLoggedIn(): boolean {
const user = JSON.parse(this.nativeStorage.getItem('user'));
return (user !== null && user.emailVerified !== false) ? true : false;
}
// Sign in with Google
// Auth logic to run auth providers
// Returns true when user's email is verified
get isEmailVerified(): boolean {
const user = JSON.parse(nativeStorage.getItem('user'));
return (user.emailVerified !== false) ? true : false;
}
}
我以前使用过localStorage,但在我使用本机存储时出现错误找不到名称'nativeStorage',它工作正常。您是说实例成员“ this.nativeStorage”吗? 如果我添加替换到this.nativeStorage 错误是'Promise'类型的参数不能分配给'string'类型的参数。
答案 0 :(得分:0)
首先,始终通过this.
引用依赖项注入变量
第二,返回类型this.nativeStorage.getItem('user')
是promise
。但是输入类型JSON.parse()
需要一个string
所以尝试下面的代码
async get isLoggedIn(): boolean {
const savedUser = await this.nativeStorage.getItem('user');
const user = savedUser ? JSON.parse(savedUser) : null;
return (user !== null && user.emailVerified !== false) ? true : false;
}