Skip to content

Instantly share code, notes, and snippets.

@DingWeizhe
Last active July 30, 2018 04:17
Show Gist options
  • Save DingWeizhe/98cfc3784d667d9225347284b570bcde to your computer and use it in GitHub Desktop.
Save DingWeizhe/98cfc3784d667d9225347284b570bcde to your computer and use it in GitHub Desktop.
Rxjs
public company$ = this.tokenService.account$
.pipe(flatMap(account => account ? this._fetchCompany(account.companyId) : null))
.pipe(shareReplay());
<ng-template let-company ngIf="companyService.company$ | async" ngIfElse="loading">
<div> {{ company.name }} </div>
</ng-template>
<ng-template #loading>
<div> 資料處理中請稍候 </div>
</ng-template>
public token$ = new BehaviorSubject<string>("");
public account$ = this.token$
.pipe(map(token => this._decodeToken(token)))
.pipe(shareReplay(1));
async public login(account: string, password: string) {
let token = await this._fetchAccount(account, password);
if (!token) throw "帳號或密碼錯誤";
this.token$.next(token);
}
public logout () {
this.token$.next("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment