ionic2.0消息订阅监听机制
2019独角兽企业重金招聘Python工程师标准>>>
两种方案: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;}});
转载于:https://my.oschina.net/u/241110/blog/719290
ionic2.0消息订阅监听机制相关推荐
- Ⅵ:zookeeper的Watcher事件监听机制
2021最新zookeeper系列 ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤ ...
- 【转载】SpringBoot 事件发布监听机制使用
前言 这一篇从应用角度来跟大伙讲讲 这个 spring 事件监听机制 ,顺便涉及到那些我认为大家应该一块了解的,我也会展开说说. 文章内容(包括不限于) : 1. 对比观察者模式 2. 应用场景的分析 ...
- SpringBoot监听redis订阅监听和发布订阅
前言 我们可以在redis中发布一条订阅到通道中,所有监听了这个通道的都可以收到这个发布的内容! redis订阅监听配置类 代码如下: RedisListenerConfig.java package ...
- 关于事件监听机制的总结(Listener和Adapter)
记得以前看过事件监听机制背后也是有一种设计模式的.(设计模式的名字记不清了,只记得背后实现的数据结构是数组.) 附上事件监听机制的分析图: 一个事件源可以承载多个事件(只要这个事件源支持这个事件就可以 ...
- 事件监听机制(一)Java事件监听
事件监听机制(一)Java事件监听 事件监听实现流程 事件对象: 继承自java.util.EventObject对象,由开发者自行定义实现. 事件源: 就是触发事件的源头,不同的事件源会触发不同的事 ...
- springBoot启动事件监听机制
springBoot启动之事件监听机制源码解析 1. Java的事件监听机制 在进行正式的分析之前,先介绍一下Java的事件监听机制.参考05–SpringBoot启动之事件监听机制 Java事件监听 ...
- spring中的事件监听机制
Spring event listener 介绍 example 简单原理解释 自定义事件.监听和发布 事件 监听器 发布者 测试 更加一般的事件 @EventListener原理 介绍 exampl ...
- 深入理解Spring的容器内事件发布监听机制
前言 Spring 提供了 ApplicationContext 事件机制,可以发布和监听事件,这个特性非常有用. Spring 内置了一些事件和监听器,例如在 Spring 容器启动前,Spring ...
- Zk监听机制入门和节点属性信息
zk监听主要是监听:节点创建,节点删除,节点改变.子节点改变 我这里用node-1和node-3同时连接node-1 zk客户端监听功能一般步骤: 1.客户端设置监听事件 命令:get 节点 watc ...
最新文章
- .NET Core Love gRPC
- 使用EntityFrameworkCore实现Repository, UnitOfWork,支持MySQL分库分表
- 你为什么不爱发朋友圈了?
- 面试时遇到「看门狗」脖子上挂着「时间轮」,我就问你怕不怕?
- ASP.NET教程5
- 如何自己去写一个鼠标驱动_为什么要用哈密顿采样器(Hamiltonian Monte Carlo),以及如何自己写一个...
- SPOOLing假脱机技术详细介绍
- Corba传值包含中文解决
- http协议(十)实体首部字段
- 在ant中将依赖jar包一并打包的方法
- 父与子的python之旅_《父与子的编程之旅:与小卡特一起学Python》作者:Warren Sande - kindle电子书下载 - 我的书库...
- SVPWM分析、各个扇区详细计算以及Matlab仿真
- 正态总体统计量的分布
- 基于SSM的校园医务室系统
- 小程序各个文件夹的作用分类
- Java神操作之利用Mybatis的resultMap的id标签进行分组映射
- 让我摘下星星送给你_抖音摘下星星给你什么歌 抖音想摘下星星给你背景音乐歌曲名...
- 苏州项目申报公司告诉你软件著作权你不知道的还很多
- 全媒体运营师胡耀文教你:产品运营生于痛点,死于增长
- JAVA/C语言在线刷题-应声虫