一、angular项目中如何实现路由缓存

需要实现的效果,对请求的数据进行缓存,比如进入文章详情页之后点击返回,不会再调用后台数据接口;而是加载缓存中的数据,如何数据变动的情况下,可使用下拉刷新刷新页面数据。类似于vue的keep-alive效果。

将下列代码保存为app-routing.cache.ts文件并保存在app文件夹下:

import { RouteReuseStrategy, DefaultUrlSerializer, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router';
export class AppRoutingCache implements RouteReuseStrategy {
public static handlers: { [key: string]: DetachedRouteHandle } = {};
// 表示对路由允许复用
public shouldDetach(route: ActivatedRouteSnapshot): boolean {
// 默认对所有路由复用 可通过给路由配置项增加data: { keep: true }来进行选择性使用,代码如下
// if (!route.data.keep) {
// return false;
// }
return true;
}
// 当路由离开时会触发。按path作为key存储路由快照&组件当前实例对象
public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
AppRoutingCache.handlers[route.routeConfig.path] = handle;
}
// 若path在缓存中有的都认为允许还原路由
public shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!AppRoutingCache.handlers[route.routeConfig.path];
}
// 从缓存中获取快照,若无则返回null
public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
if (!route.routeConfig) {
return null;
}
return AppRoutingCache.handlers[route.routeConfig.path];
}
// 进入路由触发,判断是否同一路由
public shouldReuseRoute(future: ActivatedRouteSnapshot, current: ActivatedRouteSnapshot): boolean {
return future.routeConfig == current.routeConfig;
}
}

然后在app.module.ts中添加如下代码:

import { RouteReuseStrategy } from '@angular/router';
import { AppRoutingCache } from './app-routing.cache';

修改app.module.ts中的providers: []为如下:

providers: [{ provide: RouteReuseStrategy, useClass: AppRoutingCache }]

采用以上默认设置,则实现整个项目的缓存,如果我们要进行针对性的缓存,可以在配置路由每项时加一个data: {keep: true}的选项,然后在app-routing.cache.ts打开对应注释掉的代码即可。

二、阻止冒泡事件并传参

 <div class="right-bottom" (click)="close(item.Id);$event.stopPropagation();">关闭</div>
export class BacklogTaskComponent implements OnInit {
close(id){
console.log(id);
}
}

angular项目中遇到的问题的更多相关文章

  1. gulp 在 angular 项目中的使用

    gulp 在 angular 项目中的使用 keyword:gulp,angularjs,ng,ngAnnotate,jshint,gulpfile 最后附完整简洁的ng项目gulpfile.js 准 ...

  2. angular项目中各个文件的作用

    原文地址 https://www.jianshu.com/p/176ea79a7101 大纲 1.对angular项目中的一些文件的概述 2.对其中一些文件的详细描述 2.1.package.json ...

  3. Angular 项目中如何使用 ECharts

    在有些使用 ECharts 库的 Angular 项目中,通常除了安装 npm 包之外,还会在 angular.json 中配置 “build.options.scripts”,将 “node_mod ...

  4. angular项目中使用Primeng

    1.第一步把依赖添加到项目中 npm install primeng --save npm install @angular/animations --save npm install font-aw ...

  5. Angular项目中引入jQuery

    npm install --save jquery npm install @types/jquery --save 在对应的组件中引入 import * as $ from "jquery ...

  6. 在angular项目中使用bootstrap的tooltip插件时,报错Property 'tooltip' does no t exist on type 'JQuery<HTMLElement>的解决方法和过程

    在angular4的项目中需要使用bootstrap的tooltip插件. 1. 使用命令安装jQuery和bootstrap npm install bootstrap jquery --save ...

  7. angular项目中使用jquery的问题

    1.使用npm命令往项目中添加jQuery. npm install jquery --save 2.在你想要用jQuery的组件中添加. import * as $ from "jquer ...

  8. Angular项目中共享模块的实现

    创建share Modele:ng g m share import进来所有需要共享的模块都export出去, 一.共享CommonModule 暂时只有CommonModule,以后会有一些需要共享 ...

  9. Angular项目中核心模块core Module只加载一次的实现

    核心模块CoreModule在整个系统中只加载一次,如何实现? 创建core Modele:ng g m core 既然CoreModule是类,就有构造函数,在构造函数中进行依赖注入. export ...

随机推荐

  1. 3.3_springBoot2.1.x检索之RestHighLevelClient方式

    1.版本依赖 注意对 transport client不了解先阅读官方文档: transport client(传送门) 这里需要版本匹配,如失败查看官网或百度. pom.xml <?xml v ...

  2. 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch

    这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...

  3. Game of Taking Stones && POJ1259 /// 最大空凸包 几何+DP

    题目大意: 给定n个点 求出这n个点中最大空凸包的面积 只放个模板 一份模板过两题(滑稽 这个讲解够详细了 https://blog.csdn.net/nyroro/article/details/4 ...

  4. Read-Write lock 看可以,不过看的时候不能写

    当线程“读取”实例的状态时,实例的状态不会改变,只有线程对实例“写入”操作时才会改变.read-write lock 模式将读取和写入分开来处理,在读取数据前获取读锁定,而写入之前,必须获取写锁定. ...

  5. drools原生drl规则文件的使用

    在初识drools中对drl文件进行了简单的介绍.这里举个例子来具体说明下.主要是写了规则之后我们如何用java代码来run起来. drl文件内容如下: rule "ageUp12" ...

  6. WebApi 路由机制剖析

    阅读目录 一.MVC和WebApi路由机制比较 1.MVC里面的路由 2.WebApi里面的路由 二.WebApi路由基础 1.默认路由 2.自定义路由 3.路由原理 三.WebApi路由过程 1.根 ...

  7. vuex 基本使用

    vuex:同一状态(全局状态)管理,简单的理解,在SPA单页面组件的开发中,在state中定义了一个数据之后,你可以在所在项目中的任何一个组件里进行获取.修改,并且你的修改可以同步全局. 1,安装 n ...

  8. Windows 隐藏控制台

    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...

  9. 如何在CRichEditCtrl控件中直接读如RTF格式的文件(这个是通过流的方式来读取文件)

    如何在CRichEditCtrl控件中直接读如RTF格式的文件   Inserting an RTF string using StreamIn   ------------------------- ...

  10. 线程池_ThreadPool

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...