兩種方案:一、session、watchCurrentCustomer().subscribe(data => {});訂閱機制;二、events機制.下面將以代碼重現session
方案一:app
import {Injectable} from '@angular/core';ionic
import 'rxjs/Rx';ide
import {LocalStorage, Storage} from "ionic-angular/index";this
import {Subject} from "rxjs/Subject";code
import {Customer} from "../models/Customer";rxjs
import {BehaviorSubject} from "rxjs/BehaviorSubject";get
import {Observable} from "rxjs/Observable";string
@Injectable() export class XXXXXSession extends Observable<Customer>{ private local:Storage; private currentCustomer:Subject<Customer> = new BehaviorSubject<Customer>(null); private customer; private residence;it
constructor(appConfig:AppConfig) { this.local = new Storage(LocalStorage); this.local.get('Customer').then(cust=> { console.info(cust); if (cust != null) { this.login = true; this.customer = JSON.parse(cust); } }); this.local.get('Residence').then(residence=> { console.info(residence != null); if (residence != "") { this.residence = JSON.parse(residence); } }); } isLogin() { return this.login; } getCustomer() { return this.customer; } setCustomer(customer) { this.currentCustomer.next(customer); this.local.set('Customer', JSON.stringify(customer)); this.login = true; } getCustomerId() { if (this.customer != null) { return this.customer.customerId; } return 1; } getResidence() { return this.residence; } getResidenceId() { if (this.residence != null) { return this.residence.residenceId; } return 1; } setResidence(residence) { this.local.set('Residence', JSON.stringify(residence)); this.residence = residence; }
}
監聽者: XXXXSession.watchCurrentCustomer().subscribe(data => {});
XXXXSession.watchCurrentCustomer().subscribe(data => { if(data == null){ this.portrait = "img/not_login.png"; this.isShop = false; this.customer = null; this.nickName = null; }else { this.customer = data.customer; this.nickName = this.customer.nickName; this.portrait = data.customer.portrait; this.queryCustomerShop(this.customer.customerId); } }); XXXXSession.watchCurrentResidence().subscribe((newResidence) => { if(newResidence!=null){ this.defaultResidence=newResidence.residenceName; }else{ } }); }
方案2、Events機制 import 'rxjs/Rx';
import {LocalStorage, Storage, Events} from "ionic-angular/index";
@Injectable() export class CustomerSession {
private static local:Storage = new Storage(LocalStorage); private static customer; private static residence; constructor(public events:Events) { } initSession() { CustomerSession.local.get('Customer').then(cust=> { if (cust != null) { CustomerSession.customer = JSON.parse(cust); this.events.publish("Customer:changed", CustomerSession.customer); } }); CustomerSession.local.get('Residence').then(residence=> { if (residence != null) { CustomerSession.residence = JSON.parse(residence); this.events.publish("Residence:changed", CustomerSession.residence); } }); }
}
其餘地方監聽: this.events.subscribe('Customer:changed', (eventData) => {
if (eventData != null && eventData.length > 0) { this.customer = eventData[0]; this.nickName = this.customer.nickName; this.portrait = this.customer.portrait; this.queryCustomerShop(this.customer.customerId); }else{ this.portrait = "img/not_login.png"; this.isShop = false; this.customer = null; this.nickName = "XXXX"; } }); this.events.subscribe('Residence:changed', (eventData) => { if (eventData != null && eventData.length > 0) { this.defaultResidence=eventData[0].residenceName; } });