ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
81 lines (68 loc) • 1.91 kB
Markdown
---
title:
zh-CN: 基础样例
en-US: Basic Usage
order: 0
---
## zh-CN
最简单的用法。
## en-US
Simplest of usage.
```ts
import { Component, OnDestroy } from '@angular/core';
import { CacheService } from '@ohayo/cache';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Subscription } from 'rxjs';
export class DemoComponent implements OnDestroy {
value: any;
key = 'demo';
private notify$: Subscription;
get newValue(): number {
return +new Date();
}
constructor(public srv: CacheService, private msg: NzMessageService) {}
getByHttp(): void {
this.srv.get(`https://randomuser.me/api/?results=1`).subscribe(res => {
this.value = res;
});
}
registerNotify(): void {
if (this.notify$) this.notify$.unsubscribe();
this.notify$ = this.srv.notify(this.key).subscribe(res => {
if (res == null) {
this.msg.success('register success');
return;
}
this.msg.warning(`"${this.key}" new status: ${res.type}`);
});
}
unRegisterNotify(): void {
this.srv.cancelNotify(this.key);
}
ngOnDestroy(): void {
if (this.notify$) this.notify$.unsubscribe();
}
}
```