We have a reoslver, which everytime we want visit '/courses' route, it will be triggered, then api will be called, data will be loaded.

import { Injectable } from "@angular/core";
import {
Resolve,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from "@angular/router";
import { Observable } from "rxjs";
import { Store } from "@ngrx/store";
import { AppState } from "../reducers";
import { CoursesAction } from "./actions-types";
import { tap, first, finalize } from "rxjs/operators";
import { adapter } from "./reducers/courses.reducers"; @Injectable()
export class CoursesResolver implements Resolve<any> {
loading = false;
constructor(private store: Store<AppState>) {} resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any> {
return this.store.pipe(
tap(() => {
if (!this.loading) {
this.loading = true;
this.store.dispatch(CoursesAction.loadAllCourse());
}
}),
// this resolve need to complete, so we can use first()
first(),
finalize(() => (this.loading = false))
);
}
}

So how to prevent reload the data if we have already loaded the data once?

Add one prop into state: 'allCoursesLoaded'

import { Course, compareCourses } from "../model/course";
import { EntityState, createEntityAdapter } from "@ngrx/entity";
import { createReducer, on } from "@ngrx/store";
import { CoursesAction } from "../actions-types";
/*
export interface CoursesState {
entities: { [key: number]: Course };
ids: number[];
}*/ export interface CoursesState extends EntityState<Course> {
/**Extend the entity here */
allCoursesLoaded: boolean
;
} export const adapter = createEntityAdapter<Course>({
sortComparer: compareCourses
// selectId: course => course.id // NgRx use 'id' by default
}); export const initCoursesState = adapter.getInitialState({
allCoursesLoaded: false
}); export const coursesReducer = createReducer(
initCoursesState,
on(CoursesAction.allCoursesLoaded, (state, action) =>
adapter.addAll(action.courses, { ...state, allCoursesLoaded: true })
)
); export const { selectAll } = adapter.getSelectors();

Now we can modify the resolver:

import { Injectable } from "@angular/core";
import {
Resolve,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from "@angular/router";
import { Observable } from "rxjs";
import { Store, select } from "@ngrx/store";
import { AppState } from "../reducers";
import { CoursesAction } from "./actions-types";
import { tap, first, finalize, filter } from "rxjs/operators";
import { adapter } from "./reducers/courses.reducers";
import { selectAllCoursesLoaded } from "./courses.selectors"; @Injectable()
export class CoursesResolver implements Resolve<any> {
loading = false;
constructor(private store: Store<AppState>) {} resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any> {
return this.store.pipe(
select(selectAllCoursesLoaded),
tap(courseLoaded => {
if (!this.loading && !courseLoaded) {
this.loading = true;
this.store.dispatch(CoursesAction.loadAllCourse());
}
}),
// this resolve need to complete, so we can use first()
filter(courseLoaded => courseLoaded),
first(),
finalize(() => (this.loading = false))
);
}
}

[NgRx] NgRx Data Fetching Solution - How to Load Data Only If Needed的更多相关文章

  1. LOAD DATA INFILE Syntax--官方

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...

  2. mysql迁移-----拷贝mysql目录/load data/mysqldump/into outfile

    摘要:本文简单介绍了mysql的三种备份,并解答了有一些实际备份中会遇到的问题.备份恢复有三种(除了用从库做备份之外), 直接拷贝文件,load data 和 mysqldump命令.少量数据使用my ...

  3. JDBC使用MYSQL的LOAD DATA LOACAL INFILE和LOAD DATA INFILE

    MYSQL的LOAD方法都必须建立在mysql服务允许使用该命令的情况下: 开启该命令的方法: 1.在实例对应的my.cnf(windows为my.ini)中添加一行local-infile=1(默认 ...

  4. MySQL基础之---mysqlimport工具和LOAD DATA命令导入文本文件

     1.mysqlimport工具的使用 看一下命令的使用方法: shell > mysqlimport -u root -p [--LOCAL] DBname File [option] --f ...

  5. load data infile出现“ERROR 13 (HY000): Can't get stat of '/tmp/test2.txt' (Errcode: 2)”问题

    用load data infile导数据到mysql数据库出现这个该问题,解决方法如下: 安全起见,连接mysql的语句需要添加–local-infile, mysql -hlocalhost -ur ...

  6. mysql数据库LOAD DATA INFILE Syntax

    1.LOAD DATA INFILE用来把一个文本文件里的内容高速写入到MySQL表里,它和SELECT ... INTO FILE的操作是对应的,一个导入.一个导出.使用LOAD DATA INFI ...

  7. MySQL 之 LOAD DATA INFILE 快速导入数据

    SELECT INTO OUTFILE > help select; Name: 'SELECT' Description: Syntax: SELECT [ALL | DISTINCT | D ...

  8. mysql load data 乱码

    解决方案: http://stackoverflow.com/questions/26256421/sql-load-data-infile-utf8-issue 即: load data local ...

  9. Mybatis拦截器 mysql load data local 内存流处理

    Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...

随机推荐

  1. 为什么Apache Kafka如此受欢迎

    1.目标 今天,在这个Kafka教程中,我们将学习所有Kafka功能,如可扩展性,可靠性,耐用性,这些都说明了Kafka如此受欢迎的原因.我们将详细讨论Kafka的每个功能.但在那之前让我们明白什么是 ...

  2. 一天一个Linux命令,第三天cat命令

    命令:cat (中文猫的意思) 解释:cat命令连接文件并打印到标准输出设备上(如显示器),cat经常用来显示文件的内容 注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容. ...

  3. R学习笔记1 介绍R的使用

    R脚本的一次执行叫做一个会话(Session),可以通过函数quit()退出当前的会话 quit(save = "default", status = 0, runLast = T ...

  4. [洛谷P5377][THUPC2019]鸽鸽的分割

    题目大意:有一个圆,圆上有$n$个点,将这几个点两两连接,问最多分成几部分 题解:发现这相当于一个平面图,由欧拉公式得($F$为平面分割块数,$E$为平面图边数,$V$为平面图点数):$$F=E-V+ ...

  5. html中实现某区域内右键自定义菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. JAVA MyBybatis分页

    java: import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; impo ...

  7. git push proxy 取消不掉 can not prox....

    使用这个折腾了半天 git config --global --unset http.proxy git config --global --unset https.proxy 没用,原来实现项目目录 ...

  8. sql语句将图片插入image类型的字段中

    update table set photo=(SELECT * FROM OPENROWSET(BULK N'D:\no.png', SINGLE_BLOB) as Photo)    From:h ...

  9. ASP.NET Core 过滤器中使用依赖注入

    如何给过滤器ActionFilterAttribute也用上构造函数注入呢? 一般自定义的过滤器直接用特性方式标识就能使用 [ContentFilter] 因为构造函数在使用的时候要求传参,然后我们可 ...

  10. C语言中特殊字符含义

    字符 中文 英文 说明  \n  换行符  newline     \t      Tab键  \b    backspace  退格键