需求:

最近在做一个网上商城的项目,技术用的是Angular4.x。有一个很常见的需求是:用户在点击“我的”按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册或登录页面

解决

在这里通过Angular的路由守卫来实现该功能。

1. 配置路由信息

const routes = [
{ path: 'home', component: HomeComponent },
{ path: 'product', component: ProductComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'my', component: MyComponent },
{ path: 'login', component: LoginComponent, canActivate: [RouteguardService] },//canActivate就是路由守卫
{ path: '', redirectTo: '/home', pathMatch: 'full' }
]

2. 路由守卫条件(RouteguardService.ts)

import { Injectable, Inject } from "@angular/core";
import { DOCUMENT } from "@angular/common";
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, NavigationStart } from "@angular/router";
import userModel from "./user.model"; @Injectable()
export class RouteguardService implements CanActivate {
constructor(private router: Router, @Inject(DOCUMENT) private document: any) {
} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { // this.setCookie("userId", "123456", 10);
//读取cookie
var cookies = this.document.cookie.split(";");
var userInfo = { userId: "", pw: "" };
if (cookies.length > 0) {
for (var cookie of cookies) {
if (cookie.indexOf("userId=") > -1) {
userModel.accout = cookie.split("=")[0];
userModel.password = cookie.split("=")[1];
userModel.isLogin = false;
}
}
} //获取当前路由配置信息
var path = route.routeConfig.path;
if (path == "login") {
if (!userModel.isLogin) {
//读取cookie如果没有用户信息,则跳转到当前登录页
return true;
} else {
//如果已经登录了则跳转到个人信息页面,下面语句是通过ts进行路由导航的
this.router.navigate(['product'])
}
} } setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
}

Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面的更多相关文章

  1. Vue路由守卫之路由独享守卫

    ​ 路由独立守卫,顾名思义就是这个路由自己的守卫任务,就如同咱们LOL,我们守卫的就是独立一条路,保证我们这条路不要被敌人攻克(当然我们也得打团配合) 在官方定义是这样说的:你可以在路由配置上直接定义 ...

  2. angular4.0 路由守卫详解

    在企业应用中权限.复杂页多路由数据处理.进入与离开路由数据处理这些是非常常见的需求. 当希望用户离开一个正常编辑页时,要中断并提醒用户是否真的要离开时,如果在Angular中应该怎么做呢? 其实Ang ...

  3. vue-router钩子函数实现路由守卫

    接上一篇,我们一起学习了vue路由的基本使用以及动态路由.路由嵌套以及路由命名等知识,今天我们一起来学习记录vue-router的钩子函数实现路由守卫: 何为路由守卫?路由守卫有点类似于ajax的请求 ...

  4. Angular 路由守卫

    1. 路由 Angular路由: 可以控制页面跳转:可以在多视图间切换: 2. 路由守卫 Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开.进入某路由::: return true ...

  5. Vue | 路由守卫面试常考

    前言 最近在整理基础,欢迎掘友们一起交流学习 结尾有彩蛋哦! Vue Router 路由守卫 导图目录 路由守卫分类 全局路由守卫 单个路由守卫 组件路由守卫 路由守卫执行的完整过程 路由守卫分类 全 ...

  6. vue2.0 实现导航守卫(路由守卫)

    路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...

  7. Angular4学习笔记(三)- 路由

    路由简介 路由是 Angular 应用程序的核心,它加载与所请求路由相关联的组件,以及获取特定路由的相关数据.这允许我们通过控制不同的路由,获取不同的数据,从而渲染不同的页面. 相关的类 Routes ...

  8. vue路由守卫应用,监听是否登录

    路由跳转前做一些验证,比如登录验证,是网站中的普遍需求. 对此,vue-route 提供的 beforeRouteUpdate 可以方便地实现导航守卫(navigation-guards). 导航守卫 ...

  9. react router @4 和 vue路由 详解(八)vue路由守卫

    完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 13.vue路由守卫 a.beforeEach 全局守卫 (每个路由调用前都会触发,根据 ...

随机推荐

  1. Ajax分页 Spring MVC + Hibernate

    效果图: 1.  添加公共类.方法.代码 1.    分页类:Page.java package cn.com.aperfect.sso.base.dao; import java.util.Arra ...

  2. Leetcode_118_Pascal's Triangle

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41827325 Given numRows, generat ...

  3. SMO实现

    #include "stdio.h" #include <vector> using namespace std; float function(float alfa[ ...

  4. Gradle 1.12用户指南翻译——第二十二章. 标准的 Gradle 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  5. This version of the rendering library is more recent than your version of ADT plug-in. Please update

    预览layout.xml文件时提示: This version of the rendering library is more recent than your version of ADT plu ...

  6. D-BUS详细分析

    转:http://blog.csdn.net/yclzh0522/article/details/7090599 一.概述 官方网站:http://www.freedesktop.org/wiki/S ...

  7. C++之标准输入输出

    由于在公司,无法上传图片,往后补上. 关于C++的标准输入输出,其实就相当于C语言的scanf和printf,只不过C++用cin和cout这样称为流的机制. #include <iostrea ...

  8. C++语言之类class

    在现实世界中,经常有属于同一类的对象.例如,你的自行车只是世界上很多自行车中的一辆.在面向对象软件中,也有很多共享相同特征的不同的对象:矩形.雇用记录.视频剪辑等.可以利用这些对象的相同特征为它们建立 ...

  9. myBatis源码学习之SqlSession

    在上一篇文章中SqlSessionFactory介绍了生产SqlSession的工厂,SqlSession是一个接口其具体实现类为DefaultSqlSession,SqlSession接口主要定义了 ...

  10. 配置SharePoint环境加域提示网络名不可用[已解决]

    今天去客户给机器做备机,带着装好SharePoint07的机器跑过去了,先做个LAN,然后连上机器开始工作:首先当然是改ip地址,然后都改好了开始加域,加了好几次,发现都不行,提示"指定的网 ...