angular 2+ 路由守卫
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<fromReducer.State>) {
}
// canActivate: 控制是否允许进入路由。
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
return this.activate();
}
// 在登陆的时候会把登陆信息存到浏览器localStorage,退出登陆时remove掉,如果是直接打开地址而没有登陆的话,会跳到登陆界面,judgeuser是请求用户信息接口,setUserInfo()把请求到的信息存到浏览器
activate(): Observable<boolean> {
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 { }
angular 2+ 路由守卫的更多相关文章
- Angular 4 路由守卫
路由守卫 只有当用户已经登录并拥有某些权限时才能进入某些路由 一个有多个表单组成的向导,如注册流程,用户只有在当前组件的组件中填写了满足要求的信息才可以导航到下一个路由 当用户未执行保存操作而试图离开 ...
- Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面
需求: 最近在做一个网上商城的项目,技术用的是Angular4.x.有一个很常见的需求是:用户在点击"我的"按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册 ...
- angular路由详解六(路由守卫)
路由守卫 CanActivate: 处理导航到某个路由的情况. CanDeactivate:处理从当前路由离开的情况. Resole:在路由激活之前获取路由数据. 1.CanActivate: 处理导 ...
- angular路由守卫
路由守卫是指当用户满足了某些要求之后才可以离开或者进入某个页面或者场景的时候使用.比如说只有当用户填写了用户名和密码之后才可以进入首页,比如说用户离开某个页面时明月保存信息提示用户是否保存信息后再离 ...
- Angular路由——路由守卫
一.路由守卫 当用户满足一定条件才被允许进入或者离开一个路由. 路由守卫场景: 只有当用户登录并拥有某些权限的时候才能进入某些路由. 一个由多个表单组成的向导,例如注册流程,用户只有在当前路由的组件中 ...
- Angular路由守卫 canActivate
作用 canActivate 控制是否允许进入路由. canActivateChild 等同 canActivate,只不过针对是所有子路由. 关键代码 创建路由守卫 import { Injecta ...
- Angular路由守卫 canDeactivate
目的 离开页面时,做出逻辑判断 以ng-alain的项目为基础做演示 效果如图: 关键代码 定义一个CanDeactivateGuardService export class CanDeactiva ...
- Angular 路由守卫
1. 路由 Angular路由: 可以控制页面跳转:可以在多视图间切换: 2. 路由守卫 Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开.进入某路由::: return true ...
- Angular 从入坑到挖坑 - 路由守卫连连看
一.Overview Angular 入坑记录的笔记第六篇,介绍 Angular 路由模块中关于路由守卫的相关知识点,了解常用到的路由守卫接口,知道如何通过实现路由守卫接口来实现特定的功能需求,以及实 ...
随机推荐
- 停掉一台服务器,Nginx响应慢(转载)
测试发现的问题及解决办法 1.当后端两台IIS应用服务器都正常时,访问速度非常快,查看日志,原来一个请求,是后端两台服务器同时响应的; 2.为了模仿故障测试,停掉一台IIS应用服务器,这时再访问,请求 ...
- java web中get请求中文乱码在filter中解决
之前已经讲过get或者post方法的中文乱码问题,之前都是在每个方法中编写设置编码.如果程序变大,就会很繁琐,使用filter可以避免这种繁琐. 1)写一个encodingFilter进行编码设置 p ...
- maple minimax函数
numapprox[minimax] - minimax rational approximation Calling Sequence minimax(f, x=a..b, [m, n], w, ...
- CSectsInfomation.cpp文件
#include "SectsInfomation.h" #include "WidgetMgr.h" #include "XButton.h&quo ...
- python生成器,函数,数组
1.什么是生成器用一个比喻来形容,工厂中生产保龄球的流水线,机器每次只生产一个保龄球,下次继续生产下一个,直到停止(原料不足,停止供电等条件)为止.机器就是我们的生成器. 2.使用示例在python中 ...
- Netty 源码分析之 番外篇 Java NIO 的前生今世
简介 Java NIO 是由 Java 1.4 引进的异步 IO. Java NIO 由以下几个核心部分组成: Channel Buffer Selector NIO 和 IO 的对比 IO 和 NI ...
- systemd启动多实例
最近用了centos7,启动管理器用的是systemd,感觉很好玩. 1.开机自动启动 新建一个service文件放到/usr/lib/systemd/system/ 比如: [Unit] Descr ...
- Ajax的两个用法
1.实现的效果是:通过一个函数,里面调用Ajax,函数的返回值是Ajax成功调用之后得到的返回值. 用jQuery进行简单的演示: function getRobotInfo(id) { var ip ...
- 70个shell经常使用操作
1) 怎样向脚本传递參数 ? ./script argument 样例: 显示文件名脚本 ./show.sh file1.txt cat show.sh #!/bin/bash echo $1 2) ...
- HTTP Content-Disposition Explanation [ from MDN ]
在常规的HTTP应答中,Content-Disposition 消息头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地. 在multipart ...