欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

angular狀態(tài)管理器NgRx怎么用

本篇內(nèi)容主要講解“angular狀態(tài)管理器NgRx怎么用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“angular狀態(tài)管理器NgRx怎么用”吧!

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了龍泉免費(fèi)建站歡迎大家使用!

NgRx 是 Angular 應(yīng)用中實(shí)現(xiàn)全局狀態(tài)管理的 Redux 架構(gòu)解決方案。

angular狀態(tài)管理器NgRx怎么用

  • @ngrx/store:全局狀態(tài)管理模塊

  • @ngrx/effects:處理副作用

  • @ngrx/store-devtools:瀏覽器調(diào)試工具,需要依賴 Redux Devtools Extension

  • @ngrx/schematics:命令行工具,快速生成 NgRx 文件

  • @ngrx/entity:提高開發(fā)者在 Reducer 中操作數(shù)據(jù)的效率

  • @ngrx/router-store:將路由狀態(tài)同步到全局 Store

快速開始

1、下載 NgRx

npm install @ngrx/store @ngrx/effects @ngrx/entity @ngrx/router-store @ngrx/store-devtools @ngrx/schematics

2、配置 NgRx CLI

ng config cli.defaultCollection @ngrx/schematics

// angular.json
"cli": {
  "defaultCollection": "@ngrx/schematics"
}

3、創(chuàng)建 Store

ng g store State --root --module app.module.ts --statePath store --stateInterface AppState

4、創(chuàng)建 Action

ng g action store/actions/counter --skipTests

import { createAction } from "@ngrx/store"

export const increment = createAction("increment")
export const decrement = createAction("decrement")

5、創(chuàng)建 Reducer

ng g reducer store/reducers/counter --skipTests --reducers=../index.ts

import { createReducer, on } from "@ngrx/store"
import { decrement, increment } from "../actions/counter.actions"

export const counterFeatureKey = "counter"

export interface State {
  count: number
}

export const initialState: State = {
  count: 0
}

export const reducer = createReducer(
  initialState,
  on(increment, state => ({ count: state.count + 1 })),
  on(decrement, state => ({ count: state.count - 1 }))
)

6、創(chuàng)建 Selector

ng g selector store/selectors/counter --skipTests

import { createFeatureSelector, createSelector } from "@ngrx/store"
import { counterFeatureKey, State } from "../reducers/counter.reducer"
import { AppState } from ".."

export const selectCounter = createFeatureSelector<AppState, State>(counterFeatureKey)
export const selectCount = createSelector(selectCounter, state => state.count)

7、組件類觸發(fā) Action、獲取狀態(tài)

import { select, Store } from "@ngrx/store"
import { Observable } from "rxjs"
import { AppState } from "./store"
import { decrement, increment } from "./store/actions/counter.actions"
import { selectCount } from "./store/selectors/counter.selectors"

export class AppComponent {
  count: Observable<number>
  constructor(private store: Store<AppState>) {
    this.count = this.store.pipe(select(selectCount))
  }
  increment() {
    this.store.dispatch(increment())
  }
  decrement() {
    this.store.dispatch(decrement())
  }
}

8、組件模板顯示狀態(tài)

<button (click)="increment()">+</button>
<span>{{ count | async }}</span>
<button (click)="decrement()">-</button>

Action Payload

1、在組件中使用 dispatch 觸發(fā) Action 時傳遞參數(shù),參數(shù)最終會被放置在 Action 對象中。

this.store.dispatch(increment({ count: 5 }))

2、在創(chuàng)建 Action Creator 函數(shù)時,獲取參數(shù)并指定參數(shù)類型。

import { createAction, props } from "@ngrx/store"
export const increment = createAction("increment", props<{ count: number }>())
export declare function props<P extends object>(): Props<P>;

3、在 Reducer 中通過 Action 對象獲取參數(shù)。

export const reducer = createReducer(
  initialState,
  on(increment, (state, action) => ({ count: state.count + action.count }))
)

MetaReducer

metaReducer 是 Action -> Reducer 之間的鉤子,允許開發(fā)者對 Action 進(jìn)行預(yù)處理 (在普通 Reducer 函數(shù)調(diào)用之前調(diào)用)。

function debug(reducer: ActionReducer<any>): ActionReducer<any> {
  return function (state, action) {
    return reducer(state, action)
  }
}

export const metaReducers: MetaReducer<AppState>[] = !environment.production
  ? [debug]
  : []

Effect

需求:在頁面中新增一個按鈕,點(diǎn)擊按鈕后延遲一秒讓數(shù)值增加。

1、在組件模板中新增一個用于異步數(shù)值增加的按鈕,按鈕被點(diǎn)擊后執(zhí)行 increment_async 方法

<button (click)="increment_async()">async</button>

2、在組件類中新增 increment_async 方法,并在方法中觸發(fā)執(zhí)行異步操作的 Action

increment_async() {
  this.store.dispatch(increment_async())
}

3、在 Action 文件中新增執(zhí)行異步操作的 Action

export const increment_async = createAction("increment_async")

4、創(chuàng)建 Effect,接收 Action 并執(zhí)行副作用,繼續(xù)觸發(fā) Action

ng g effect store/effects/counter --root --module app.module.ts --skipTests

Effect 功能由 @ngrx/effects 模塊提供,所以在根模塊中需要導(dǎo)入相關(guān)的模塊依賴

import { Injectable } from "@angular/core"
import { Actions, createEffect, ofType } from "@ngrx/effects"
import { increment, increment_async } from "../actions/counter.actions"
import { mergeMap, map } from "rxjs/operators"
import { timer } from "rxjs"

// createEffect
// 用于創(chuàng)建 Effect, Effect 用于執(zhí)行副作用.
// 調(diào)用方法時傳遞回調(diào)函數(shù), 回調(diào)函數(shù)中返回 Observable 對象, 對象中要發(fā)出副作用執(zhí)行完成后要觸發(fā)的 Action 對象
// 回調(diào)函數(shù)的返回值在 createEffect 方法內(nèi)部被繼續(xù)返回, 最終返回值被存儲在了 Effect 類的屬性中
// NgRx 在實(shí)例化 Effect 類后, 會訂閱 Effect 類屬性, 當(dāng)副作用執(zhí)行完成后它會獲取到要觸發(fā)的 Action 對象并觸發(fā)這個 Action

// Actions
// 當(dāng)組件觸發(fā) Action 時, Effect 需要通過 Actions 服務(wù)接收 Action, 所以在 Effect 類中通過 constructor 構(gòu)造函數(shù)參數(shù)的方式將 Actions 服務(wù)類的實(shí)例對象注入到 Effect 類中
// Actions 服務(wù)類的實(shí)例對象為 Observable 對象, 當(dāng)有 Action 被觸發(fā)時, Action 對象本身會作為數(shù)據(jù)流被發(fā)出

// ofType
// 對目標(biāo) Action 對象進(jìn)行過濾.
// 參數(shù)為目標(biāo) Action 的 Action Creator 函數(shù)
// 如果未過濾出目標(biāo) Action 對象, 本次不會繼續(xù)發(fā)送數(shù)據(jù)流
// 如果過濾出目標(biāo) Action 對象, 會將 Action 對象作為數(shù)據(jù)流繼續(xù)發(fā)出

@Injectable()
export class CounterEffects {
  constructor(private actions: Actions) {
    // this.loadCount.subscribe(console.log)
  }
  loadCount = createEffect(() => {
    return this.actions.pipe(
      ofType(increment_async),
      mergeMap(() => timer(1000).pipe(map(() => increment({ count: 10 }))))
    )
  })
}

Entity

1、概述

Entity 譯為實(shí)體,實(shí)體就是集合中的一條數(shù)據(jù)。

NgRx 中提供了實(shí)體適配器對象,在實(shí)體適配器對象下面提供了各種操作集合中實(shí)體的方法,目的就是提高開發(fā)者操作實(shí)體的效率。

2、核心

1、EntityState:實(shí)體類型接口

/*
	{
		ids: [1, 2],
		entities: {
			1: { id: 1, title: "Hello Angular" },
			2: { id: 2, title: "Hello NgRx" }
		}
	}
*/
export interface State extends EntityState<Todo> {}

2、createEntityAdapter: 創(chuàng)建實(shí)體適配器對象

3、EntityAdapter:實(shí)體適配器對象類型接口

export const adapter: EntityAdapter<Todo> = createEntityAdapter<Todo>()
// 獲取初始狀態(tài) 可以傳遞對象參數(shù) 也可以不傳
// {ids: [], entities: {}}
export const initialState: State = adapter.getInitialState()

3、實(shí)例方法

https://ngrx.io/guide/entity/adapter#adapter-collection-methods

4、選擇器

// selectTotal 獲取數(shù)據(jù)條數(shù)
// selectAll 獲取所有數(shù)據(jù) 以數(shù)組形式呈現(xiàn)
// selectEntities 獲取實(shí)體集合 以字典形式呈現(xiàn)
// selectIds 獲取id集合, 以數(shù)組形式呈現(xiàn)
const { selectIds, selectEntities, selectAll, selectTotal } = adapter.getSelectors();
export const selectTodo = createFeatureSelector<AppState, State>(todoFeatureKey)
export const selectTodos = createSelector(selectTodo, selectAll)

Router Store

1、同步路由狀態(tài)

1)引入模塊

import { StoreRouterConnectingModule } from "@ngrx/router-store"

@NgModule({
  imports: [
    StoreRouterConnectingModule.forRoot()
  ]
})
export class AppModule {}

2)將路由狀態(tài)集成到 Store

import * as fromRouter from "@ngrx/router-store"

export interface AppState {
  router: fromRouter.RouterReducerState
}
export const reducers: ActionReducerMap<AppState> = {
  router: fromRouter.routerReducer
}

2、創(chuàng)建獲取路由狀態(tài)的 Selector

// router.selectors.ts
import { createFeatureSelector } from "@ngrx/store"
import { AppState } from ".."
import { RouterReducerState, getSelectors } from "@ngrx/router-store"

const selectRouter = createFeatureSelector<AppState, RouterReducerState>(
  "router"
)

export const {
  // 獲取和當(dāng)前路由相關(guān)的信息 (路由參數(shù)、路由配置等)
  selectCurrentRoute,
  // 獲取地址欄中 # 號后面的內(nèi)容
  selectFragment,
  // 獲取路由查詢參數(shù)
  selectQueryParams,
  // 獲取具體的某一個查詢參數(shù) selectQueryParam('name')
  selectQueryParam,
  // 獲取動態(tài)路由參數(shù)
  selectRouteParams,
 	// 獲取某一個具體的動態(tài)路由參數(shù) selectRouteParam('name')
  selectRouteParam,
  // 獲取路由自定義數(shù)據(jù)
  selectRouteData,
  // 獲取路由的實(shí)際訪問地址
  selectUrl
} = getSelectors(selectRouter)
// home.component.ts
import { select, Store } from "@ngrx/store"
import { AppState } from "src/app/store"
import { selectQueryParams } from "src/app/store/selectors/router.selectors"

export class AboutComponent {
  constructor(private store: Store<AppState>) {
    this.store.pipe(select(selectQueryParams)).subscribe(console.log)
  }
}

到此,相信大家對“angular狀態(tài)管理器NgRx怎么用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)站名稱:angular狀態(tài)管理器NgRx怎么用
文章路徑:http://chinadenli.net/article26/joehcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站內(nèi)鏈、小程序開發(fā)、定制網(wǎng)站、品牌網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
少妇丰满a一区二区三区| 好吊日成人免费视频公开| 亚洲国产精品一区二区| 人妻熟女中文字幕在线| 日本精品啪啪一区二区三区| 福利一区二区视频在线| 好吊一区二区三区在线看| 国产精品免费视频久久| 国产精品超碰在线观看| 天堂网中文字幕在线观看| 久久精品色妇熟妇丰满人妻91| 婷婷一区二区三区四区| 五月婷婷综合缴情六月| 美女黄色三级深夜福利| 九九热精品视频免费在线播放| 伊人天堂午夜精品草草网| 国产又色又爽又黄的精品视频| 午夜福利视频日本一区| 日韩一区欧美二区国产| 亚洲乱妇熟女爽的高潮片| 中文字幕一区二区免费| 亚洲一级在线免费观看| 日韩成人午夜福利免费视频 | 日本人妻精品有码字幕| 极品熟女一区二区三区| 亚洲av又爽又色又色| 欧美日韩精品综合在线| 色老汉在线视频免费亚欧| 国产内射在线激情一区| 激情中文字幕在线观看| 日韩av亚洲一区二区三区| 久久99青青精品免费观看| 久久久精品日韩欧美丰满| 亚洲国产成人久久99精品| 欧美午夜视频免费观看| 天堂av一区一区一区| 久久久精品日韩欧美丰满| 欧美国产精品区一区二区三区| 国产水滴盗摄一区二区| 日韩日韩日韩日韩在线| 欧美av人人妻av人人爽蜜桃 |