侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

ionic2.0消息订阅监听机制

2024-05-06 星期一 / 0 评论 / 0 点赞 / 58 阅读 / 4454 字

两种方案:1、session、watchCurrentCustomer().subscribe(data => {});订阅机制;2、events机制.下面将以代码重现方案一:import {Inje

两种方案:1、session、watchCurrentCustomer().subscribe(data => {});订阅机制;2、events机制.下面将以代码重现

方案一:

import {Injectable} from '@angular/core';

import 'rxjs/Rx';

import {LocalStorage, Storage} from "ionic-angular/index";

import {Subject} from "rxjs/Subject";

import {Customer} from "../models/Customer";

import {BehaviorSubject} from "rxjs/BehaviorSubject";

import {Observable} from "rxjs/Observable";

@Injectable()export class XXXXXSession extends Observable<Customer>{private local:Storage;private currentCustomer:Subject<Customer> = new BehaviorSubject<Customer>(null);private customer;private residence;

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{            }        });            }

方案二、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;        }    });

广告 广告

评论区