Simple Auth service:

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {User} from '../model/user';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/map'; export const ANONYMOUS_USER: User = {
id: undefined,
email: ''
}; @Injectable()
export class AuthService { subject = new BehaviorSubject<User>(ANONYMOUS_USER);
user$: Observable<User> = this.subject.asObservable();
isLoggedIn$: Observable<boolean> = this.user$.map(user => !!user.id);
isLoggedOut$: Observable<boolean> = this.isLoggedIn$.map(isLoggedIn => !isLoggedIn); constructor(private http: HttpClient) { } signUp(email: string, password: string) { } }

Using Observable is a easy way to implement reactive application.

Create a BehaviorSubject and then convert subject to Observable. BehaviorSubject also takes a init param.

The isLoggedIn$ & isLoggedOut$ based on user$.

The in the component, we inject the auth service:

import {Component, OnInit} from '@angular/core';
import {AuthService} from './services/auth.service';
import {Observable} from 'rxjs/Observable';
import {User} from './model/user'; @Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit { user$: Observable<User>;
isLoggedIn$: Observable<boolean>;
isLoggedOut$: Observable<boolean>; constructor(private auth: AuthService) { } ngOnInit() {
this.user$ = this.auth.user$;
this.isLoggedIn$ = this.auth.isLoggedIn$;
this.isLoggedOut$ = this.auth.isLoggedOut$;
}
}

HTML:

        <li *ngIf="isLoggedOut$ | async">
<a routerLink="/signup">Sign Up</a>
</li>
<li *ngIf="isLoggedOut$ | async">
<a routerLink="/login">Login</a>
</li>
<li *ngIf="isLoggedIn$ | async">
<a >Logout</a>
</li>

[Angular] Design API for show / hide components based on Auth的更多相关文章

  1. Angular中文api

    Angular中文api:http://docs.ngnice.com/api

  2. angular -- $route API翻译

    $route -$routeProvider服务 -依赖ngRoute模块 $route能够在路径发生改变的时候,渲染不同的视图,调用不同的控制器.它监测了$location.url(),然后根据路径 ...

  3. 文献翻译|Design of True Random Number Generator Based on Multi-stage Feedback Ring Oscillator(基于多级反馈环形振荡器的真随机数发生器设计)

    基于多级反馈环形振荡器的真随机数发生器设计 摘要 真随机数生成器(trng)在加密系统中起着重要的作用.本文提出了一种在现场可编程门阵列(FPGA)上生成真随机数的新方法,该方法以 多级反馈环形振荡器 ...

  4. Angular JS API

    ng function angular.bind angular.bootstrap angular.copy angular.element angular.equals angular.exten ...

  5. angular -- $routeParams API翻译

    原api出处: https://docs.angularjs.org/api/ngRoute/service/$routeParams $routeParams 可以获取当前路径参数. 需要ngrou ...

  6. 前端技术之:常见前端UI相关开源项目

    Bootstrap https://getbootstrap.com/BootstrapVue provides one of the most comprehensive implementatio ...

  7. Web API design

    Web API design 28 minutes to read Most modern web applications expose APIs that clients can use to i ...

  8. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(三)

    在前面两篇文章中,我介绍了基于IdentityServer4的一个Identity Service的实现,并且实现了一个Weather API和基于Ocelot的API网关,然后实现了通过Ocelot ...

  9. View and Data API Tips: Hide elements in viewer completely

    By Daniel Du With View and Data API, you can hide some elements in viewer by calling "viewer.hi ...

随机推荐

  1. Cocos2d-x手机游戏开发与项目实践具体解释_随书代码

    Cocos2d-x手机游戏开发与项目实战具体解释_随书代码 作者:沈大海  因为原作者共享的资源为UTF-8字符编码.下载后解压在win下显示乱码或还出现文件不全问题,现完整整理,解决全部乱码问题,供 ...

  2. 深入理解javascript之高级定时器

    setTimeout()和setInterval()能够用来创建定时器.其主要的用法这里就不再做介绍了.这里主要介绍一下javascript的代码队列. 在javascript中没有不论什么代码是马上 ...

  3. UI_搭建MVC

    新建RootViewController 继承于 UIViewController 新建RootView 继承于 UIView AppDelegate.m 中引入 #import "Root ...

  4. C#中函数的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. 2017国家集训队作业[agc006f]Blackout

    2017国家集训队作业[agc006f]Blackout 题意: 有一个\(N*N\)的网格,一开始有\(M\)个格子被涂黑,给出这\(M\)个格子,和染色操作:如果有坐标为\((x,y),(y,z) ...

  6. vue2细节变化的用法

    1.v-el和v-ref合并为一个属性:ref 原来:v-el:my-element 现在: ref="myElement", v-ref:my-component 变成了这样:  ...

  7. yum---rpm软件包管理器

    yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性 ...

  8. python 中文文档地址总结

    sqlalchemy: https://www.imooc.com/article/details/id/22343

  9. ArcGIS “Error HRESULT E_FAIL has been returned from a call to a COM component.” 异常的解决

    错误提示内容: {System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been ret ...

  10. Git管理软件

    软件下载地址 首先下载库 https://code.google.com/p/msysgit/ 接着安装 https://code.google.com/p/tortoisegit/ 然后就能够用了