我是離子2的新用戶,並且存儲我的用戶數據時存在問題,該數據存儲在我的注射服務中的類中以供登錄請幫助我:如何使用ionic2框架登錄後如何存儲用戶ID
AUTH-service.ts
export class User {
nom: string;
email: string;
constructor(nom: string, email: string) {
this.nom = nom;
this.email = email;
}
}
@Injectable()
export class AuthService {
currentUser: User;
members: Array<any>;
nomUser:string;
emailUser:string;
constructor(public http: Http) {
console.log('Hello Authentification');
}
public login(credentials) {
if (credentials.email === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
return Observable.create(observer => {
var url = 'http://localhost/PFEBACKEND/login-client.php?
email='+credentials.email+'&passe='+credentials.password ;
this.http.get(url).map(res => res.json()).subscribe(
data=>{
let access = (data.error=== 0)
this.currentUser = new User(data.nom,data.email);
console.log(this.currentUser);
observer.next(access);
observer.complete();
},
err => {
console.log(err);
},
() => console.log('authentifié'));
});
}
public getUserInfo() {
return this.currentUser ;
}
home.ts
import { AuthService } from '../../providers/auth-service';
import { Http, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import { Component,Inject } from '@angular/core';
import { NavController, NavParams,LoadingController } from 'ionic-angular';
export class HomePage {
nom:string= '';
email:string= '';
constructor(public http: Http,public navCtrl: NavController, private
auth:AuthService,public data:Datamembers, public navParams:NavParams,
public loadingCtrl:LoadingController)
{
let info = this.auth.getUserInfo();
this.email = info.email;
this.nom = info.nom;
}
}
錯誤:
nav-controller-base.js:88 Failed to navigate: Cannot read property 'email' of undefined (anonymous) @ nav-controller-base.js:88
core.es5.js:1085 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'email' of undefined
謝謝soooo多!!! @sagar它的工作,但我在如何保存用戶和他們的信息後登錄??鬥爭我與clas工作s在我的身份驗證服務中將Username命名爲User,但是一旦我刷新頁面,所有的信息都已消失 –
您想在哪裏存儲它?無論如何,這個問題得到了回答。很高興幫助。 :) –
我有一個數據庫,但我不知道什麼是最好的登錄信息!!也許本地存儲!你怎麼想 ? –