博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular 2+ 路由守卫
阅读量:4565 次
发布时间:2019-06-08

本文共 3345 字,大约阅读时间需要 11 分钟。

1. 定义接口名称 /domain/login-guard.ts

export interface LoginGuard {  data: any;  msg: string;  status: boolean;}

2. 定义actions  /ngrx/actions/login-guard.action.ts

import { Action } from '@ngrx/store';import {LoginGuard} from '../../domain/login-guard';/** * For each action type in an action group, make a simple * enum object for all of this group's action types. */export enum ActionTypes {  GUARD = '[GUARD]'};/** * Every action is comprised of at least a type and an optional * payload. Expressing actions as classes enables powerful * type checking in reducer functions. */export class GuardSuccess implements Action {  readonly type = ActionTypes.GUARD;  constructor(public payload: LoginGuard) { }}/** * Export a type alias of all actions in this action group * so that reducers can easily compose action types */export type Actions  = GuardSuccess;

3. 定义reducers  /ngrx/actions/login-guard.reducer.ts

import * as fromLogin from '../actions/login-guard.action';import {LoginGuard} from '../../domain/login-guard';export interface State {  guard: LoginGuard;}export const initialState: State = {  guard: {    data: '',    msg: '',    status: true  }};export function reducer(state = initialState, action: fromLogin.Actions): State {  switch (action.type) {    case fromLogin.ActionTypes.GUARD: {      return {...state, guard: action.payload};    }    default: {      return state;    }  }}

4. login.service.ts

import {Injectable} from '@angular/core';import {ActivatedRouteSnapshot, NavigationEnd, Router, RouterStateSnapshot} from '@angular/router';import {Observable} from 'rxjs/Observable';import 'rxjs';import {HttpClient} from '@angular/common/http';import * as fromReducer from '../ngrx/reducers/index';import {Store} from "@ngrx/store";import {GuardSuccess} from '../ngrx/actions/login-guard.action';export class LoginService {  constructor(private router: Router, private http: HttpClient, private store$: Store
) { }// canActivate: 控制是否允许进入路由。 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable
| Promise
{ return this.activate(); }// 在登陆的时候会把登陆信息存到浏览器localStorage,退出登陆时remove掉,如果是直接打开地址而没有登陆的话,会跳到登陆界面,judgeuser是请求用户信息接口,setUserInfo()把请求到的信息存到浏览器 activate(): Observable
{ const url = `${environment.path}/judgeUser`; const params = JSON.parse(localStorage.getItem('LOGININFO')); const param = { emNumber:params.emNumber, emPhone:params.emPhone } return this.http.post(url, param).map((guard: LoginGuard) => { const guard$ = guard; if (!guard$.status) { this.router.navigate(['/']); } setUserInfo(guard$); this.store$.dispatch(new GuardSuccess(guard$)); return guard$.status; }); }}

5. service注入

import {LoginService} from './service/login.service';@NgModule({  declarations: [    AppComponent,    DemoComponent,    // HtmlPipe  ],  imports: [    BrowserAnimationsModule,    AppRoutingModule,    ViewModule,    ShareModule,    AppStoreModule,    HttpClientModule,    DirectivesModule,  ],  providers: [LayerService, InterfaceService, LoginService, {    provide: LocationStrategy,    useClass: HashLocationStrategy  }],  bootstrap: [AppComponent]})export class AppModule { }

 

转载于:https://www.cnblogs.com/leiting/p/9447794.html

你可能感兴趣的文章
wpa_supplicant安装
查看>>
tilemap坐标转换
查看>>
socket 事件模型
查看>>
npm 安装远程包(github的)
查看>>
WCF 重新设置服务器地址的bug
查看>>
四、条件、循环、函数定义 练习
查看>>
conky 配置变量表
查看>>
nyoj-38 布线问题
查看>>
把SQLServer数据库从高版本降级到低版本
查看>>
GREENPLUM数据库,postgresql客户端-----创建表
查看>>
PYTHON
查看>>
百度分享2.0测试实例
查看>>
浅谈SQL Server数据内部表现形式
查看>>
C#.net 货币格式转换
查看>>
mysql <-> sqlite
查看>>
EN法兰数据处理注意事项
查看>>
多线程、互斥锁、异步、GIL
查看>>
luogu P5320 [BJOI2019]勘破神机
查看>>
[No0000103]JavaScript-基础课程3
查看>>
contos7 安装netcore 和vscode
查看>>