一、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. RTC, Real Time Clock

    配置 写入RTC_PRL, RTC_CNT, RTC_ALR寄存器时,需要先进入配置模式,通过把RTC_CRL寄存器的CNF位置一. 另外,在每次配置一个寄存器时必须等待上一次配置完成,可以通过检测R ...

  2. Invalid Rom

    问题描述 原因说明 这多半是因为单片机超频被锁,就是HSE_VALUE设置的与实际值不一致,容易造成这个问题. 解放方法 1. 先正确配置 HSE_VLAUE, 看HSE_VALUE 修改问题. 2. ...

  3. Tomcat下载部署及解决中文乱码显示

    一.下载 tomcat下载链接:https://tomcat.apache.org/ 1.进入tomcat官网后,我使用的是tomcat9,所以我选择tomcat9.然后点击core下的zip包下载. ...

  4. 20140331 HOG代码调试 Boost库安装

    1.CUDAHOG代码调试 错误1: 错误提示:(main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall cudaHOG::cudaH ...

  5. linux mysql 远程访问权限问题

    1.为了让访问mysql的客户端的用户有访问权限,我们可以通过如下方式为用户进行授权:mysql> grant all on *.* to user_name@'%' identified by ...

  6. 2018北京网络赛 G The Mole /// 分块暴力 点线距离

    题目大意: 给定n段线段 编号为1~n 接下来m个询问 给定一点 输出离该点最近的线段的最小编号(距离相等时取编号小的) 题解 大致就是 1.坐标范围为(0,2^16-1) 将坐标系划分为2^8*2^ ...

  7. iOS开发之系统通讯录

                          @iOS调用操作通讯录所用的库文件                                         AddressBook.framewor ...

  8. iOS开发系列-iOS适配

    概述 发布iPhone X 系统版本为iOS11, 由于刘海屏原因需要对新的机型做适配. iPhone X safeArea iOS11苹果提出safeArea替代iOS7引入 topLayoutGu ...

  9. UC浏览器禁止图片阅读模式处理方法

    本文转载自:https://www.cnblogs.com/MY0101/p/9969818.html UC浏览器点击图片会出现图片阅读模式. 如何处理? <img style=" w ...

  10. 如何检测 Web 服务请求丢失问题

    导读 『StabilityGuide』是阿里多位阿里技术工程师共同发起的稳定性领域的知识库开源项目,涵盖性能压测.故障演练.JVM.应用容器.服务框架.流量调度.监控.诊断等多个技术领域,以更结构化的 ...