angular8和ngrx8結(jié)合使用的步驟介紹
一、案例運行后的效果圖
二、關(guān)于ngrx的認(rèn)識
2、ngrx是借鑒redux的思維,專門為angular中定制的一個狀態(tài)管理的包,類似react中的redux、vue中的vuex,主要包括以下幾個模塊(本文先介紹@ngrx/store)
- @ngrx/store
- @ngrx/store-devtools
- @ngrx/effects
- @ngrx/router-store
- @ngrx/entity
- @ngrx/data
- @ngrx/schematics
3、需要使用ngrx的場景
- 在angular項目開發(fā)中屬于非必須的
- 大項目中需要進(jìn)行組件通訊,數(shù)據(jù)共享
三、構(gòu)建項目
1、使用@angular/cli初始化項目
ng new angular-ngrx
2、創(chuàng)建一個數(shù)據(jù)的module(手動修改名字為AppStoreModule,不然會和@ngrx/store中的重名)
ng g m store
3、在store文件夾下創(chuàng)建三個文件夾
- actions
- reducers
- selectors(非必須的,僅僅是對于一個狀態(tài)樹是對象的時候,寫一個方法選擇狀態(tài)樹中的一個節(jié)點)
4、手動安裝@ngrx/store
npm install @ngrx/store --save
5、手動安裝@ngrx/store-devtools
npm install @ngrx/store-devtools --save
6、在reducers文件夾下創(chuàng)建index.ts(使用ng add @ngrx/store會默認(rèn)生成的)
import { ActionReducerMap, MetaReducer } from '@ngrx/store'; import { environment } from '../../../environments/environment'; // 項目中全部的狀態(tài) export interface State {} // 全部的reducer函數(shù) export const reducers: ActionReducerMap<State> = {}; export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
7、瀏覽器要安裝redux插件
8、在store.module.ts中配置瀏覽器調(diào)試的更多配置見
@NgModule({ declarations: [], imports: [ StoreModule.forRoot(reducers, { metaReducers, runtimeChecks: { strictStateImmutability: true, strictActionImmutability: true, strictStateSerializability: true, strictActionSerializability: true, } }), StoreDevtoolsModule.instrument({ maxAge: 20, logOnly: environment.production }) ] }) export class AppStoreModule { }
四、在項目中使用@ngrx/store
1、代碼的使用見github中的book組件
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解angular路由高亮之RouterLinkActive
這篇文章主要介紹了詳解angular路由高亮之RouterLinkActive,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04AngularJS基礎(chǔ) ng-list 指令詳解及示例代碼
本文主要介紹AngularJS ng-list 指令,這里幫大家整理了ng-list指令的基本資料,并附有示例代碼,有需要的小伙伴可以參考下2016-08-08AngularJS整合Springmvc、Spring、Mybatis搭建開發(fā)環(huán)境
這篇文章主要介紹了AngularJS整合Springmvc、Spring、Mybatis搭建開發(fā)環(huán)境的相關(guān)資料,為學(xué)習(xí)使用AngularJS做好基礎(chǔ)準(zhǔn)備,感興趣的小伙伴們可以參考一下2016-02-02微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁面跳轉(zhuǎn),jssdk校驗失敗問題解決
本文主要介紹微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁面跳轉(zhuǎn),jssdk校驗失敗問題解決,這里提供了詳細(xì)的操作方式,有需要的小伙伴可以參考下2016-09-09使用Angular9和TypeScript開發(fā)RPG游戲的方法
這篇文章主要介紹了使用Angular9和TypeScript開發(fā)RPG游戲的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03使用Angular CLI生成 Angular 5項目教程詳解
這篇文章主要介紹了使用Angular CLI生成 Angular 5項目的教程詳解 ,需要的朋友可以參考下2018-03-03詳解Angular2表單-模板驅(qū)動的表單(Template-Driven Forms)
本篇文章主要介紹了詳解Angular2表單-模板驅(qū)動的表單(Template-Driven Forms),具有一定的參考價值,有興趣的可以了解一下2017-08-08