日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ngRx 官方示例分析 - 4.pages

發(fā)布時(shí)間:2023/12/13 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ngRx 官方示例分析 - 4.pages 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?Page 中通過構(gòu)造函數(shù)注入 Store,基于 Store 進(jìn)行數(shù)據(jù)操作。

注意 Component 使用了?changeDetection: ChangeDetectionStrategy.OnPush.

OnPush?means that the change detector's mode will be set to?CheckOnce?during hydration.

?

/app/containers/collection-page.ts

import 'rxjs/add/operator/let'; import { Component, ChangeDetectionStrategy } from '@angular/core'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable';import * as fromRoot from '../reducers'; import { Book } from '../models/book';@Component({selector: 'bc-collection-page',changeDetection: ChangeDetectionStrategy.OnPush,template: `<md-card><md-card-title>My Collection</md-card-title></md-card><bc-book-preview-list [books]="books$ | async"></bc-book-preview-list> `,/*** Container components are permitted to have just enough styles* to bring the view together. If the number of styles grow,* consider breaking them out into presentational* components.*/styles: [`md-card-title {display: flex;justify-content: center;}`] }) export class CollectionPageComponent {books$: Observable<Book[]>;constructor(store: Store<fromRoot.State>) {this.books$ = store.select(fromRoot.getBookCollection);} }

/app/containers/find-book-page.ts

import 'rxjs/add/operator/let'; import 'rxjs/add/operator/take'; import { Component, ChangeDetectionStrategy } from '@angular/core'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable';import * as fromRoot from '../reducers'; import * as book from '../actions/book'; import { Book } from '../models/book';@Component({selector: 'bc-find-book-page',changeDetection: ChangeDetectionStrategy.OnPush,template: `<bc-book-search [query]="searchQuery$ | async" [searching]="loading$ | async" (search)="search($event)"></bc-book-search><bc-book-preview-list [books]="books$ | async"></bc-book-preview-list> ` }) export class FindBookPageComponent {searchQuery$: Observable<string>;books$: Observable<Book[]>;loading$: Observable<boolean>;constructor(private store: Store<fromRoot.State>) {this.searchQuery$ = store.select(fromRoot.getSearchQuery).take(1);this.books$ = store.select(fromRoot.getSearchResults);this.loading$ = store.select(fromRoot.getSearchLoading);}search(query: string) {this.store.dispatch(new book.SearchAction(query));} }

?注意,點(diǎn)擊搜索之后,我們回派發(fā)一個(gè) Search 的 Action,但是,在 Book 的 Reducer 中并不處理這個(gè) Action, @ngrx/effect 將會(huì)監(jiān)控這個(gè) Action,進(jìn)行異步處理。

/app/containers/not-found-page.ts

import { Component, ChangeDetectionStrategy } from '@angular/core';@Component({selector: 'bc-not-found-page',changeDetection: ChangeDetectionStrategy.OnPush,template: `<md-card><md-card-title>404: Not Found</md-card-title><md-card-content><p>Hey! It looks like this page doesn't exist yet.</p></md-card-content><md-card-actions><button md-raised-button color="primary" routerLink="/">Take Me Home</button></md-card-actions></md-card>`,styles: [`:host {text-align: center;}`] }) export class NotFoundPageComponent { }

?通過 @Input() 參數(shù)將數(shù)據(jù)從頁面?zhèn)鬟f給下面的 Component,事件從底層 Component 冒泡上來。

/app/containers/selected-book-page.ts

import { Component, ChangeDetectionStrategy } from '@angular/core'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable';import * as fromRoot from '../reducers'; import * as collection from '../actions/collection'; import { Book } from '../models/book';@Component({selector: 'bc-selected-book-page',changeDetection: ChangeDetectionStrategy.OnPush,template: `<bc-book-detail[book]="book$ | async"[inCollection]="isSelectedBookInCollection$ | async"(add)="addToCollection($event)"(remove)="removeFromCollection($event)"></bc-book-detail> ` }) export class SelectedBookPageComponent {book$: Observable<Book>;isSelectedBookInCollection$: Observable<boolean>;constructor(private store: Store<fromRoot.State>) {this.book$ = store.select(fromRoot.getSelectedBook);this.isSelectedBookInCollection$ = store.select(fromRoot.isSelectedBookInCollection);}addToCollection(book: Book) {this.store.dispatch(new collection.AddBookAction(book));}removeFromCollection(book: Book) {this.store.dispatch(new collection.RemoveBookAction(book));} }

?

/app/containers/view-book-page.ts

import '@ngrx/core/add/operator/select'; import 'rxjs/add/operator/map'; import { Component, OnDestroy, ChangeDetectionStrategy } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { Store } from '@ngrx/store'; import { Subscription } from 'rxjs/Subscription';import * as fromRoot from '../reducers'; import * as book from '../actions/book';/*** Note: Container components are also reusable. Whether or not* a component is a presentation component or a container* component is an implementation detail.** The View Book Page's responsibility is to map router params* to a 'Select' book action. Actually showing the selected* book remains a responsibility of the* SelectedBookPageComponent*/ @Component({selector: 'bc-view-book-page',changeDetection: ChangeDetectionStrategy.OnPush,template: `<bc-selected-book-page></bc-selected-book-page> ` }) export class ViewBookPageComponent implements OnDestroy {actionsSubscription: Subscription;constructor(store: Store<fromRoot.State>, route: ActivatedRoute) {this.actionsSubscription = route.params.select<string>('id').map(id => new book.SelectAction(id)).subscribe(store);}ngOnDestroy() {this.actionsSubscription.unsubscribe();} }

?

/app/containers/app.ts

import 'rxjs/add/operator/let'; import { Observable } from 'rxjs/Observable'; import { Component, ChangeDetectionStrategy } from '@angular/core'; import { Store } from '@ngrx/store';import * as fromRoot from '../reducers'; import * as layout from '../actions/layout';@Component({selector: 'bc-app',changeDetection: ChangeDetectionStrategy.OnPush,template: `<bc-layout><bc-sidenav [open]="showSidenav$ | async"><bc-nav-item (activate)="closeSidenav()" routerLink="/" icon="book" hint="View your book collection">My Collection</bc-nav-item><bc-nav-item (activate)="closeSidenav()" routerLink="/book/find" icon="search" hint="Find your next book!">Browse Books</bc-nav-item></bc-sidenav><bc-toolbar (openMenu)="openSidenav()">Book Collection</bc-toolbar><router-outlet></router-outlet></bc-layout> ` }) export class AppComponent {showSidenav$: Observable<boolean>;constructor(private store: Store<fromRoot.State>) {/*** Selectors can be applied with the `select` operator which passes the state* tree to the provided selector*/this.showSidenav$ = this.store.select(fromRoot.getShowSidenav);}closeSidenav() {/*** All state updates are handled through dispatched actions in 'container'* components. This provides a clear, reproducible history of state* updates and user interaction through the life of our* application.*/this.store.dispatch(new layout.CloseSidenavAction());}openSidenav() {this.store.dispatch(new layout.OpenSidenavAction());} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/haogj/p/6537286.html

總結(jié)

以上是生活随笔為你收集整理的ngRx 官方示例分析 - 4.pages的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。