2017-08-15 10 views
-3

私はAngular 4とngrx 4の新機能です。ngrx 4バージョンを使用してAngular 2 Tour of HeroをAngular 4に変換しようとしています。現時点では、ダッシュボードと英雄ページが表示された状態でアプリを起動しています。問題は、私のヒーローの誰も現れておらず、Chromeの開発ツールにエラーが表示されていないことです。私はこれを保持しているのか分かりません。任意のヒントやガイダンスをいただければ幸いです。ヒーローの配列は、角4とngrx 4を使用してブラウザに表示されません

hero.actions.ts 

import { Action } from '@ngrx/store'; 

import { Hero } from './../models/hero'; 


export const GET_HEROES = '[Hero] Get Heroes'; 
export const GET_HEROES_SUCCESS = '[Hero] Get Heroes Success'; 
export const GET_HEROES_FAIL = '[Hero] Get Heroes Fail' 
export const GET_HERO = '[Hero] Get Hero'; 
export const GET_HERO_SUCCESS = '[Hero] Get Hero Success'; 
export const GET_HERO_FAIL = '[Hero] Get Hero Fail'; 
export const CREATE_HERO = '[Hero] Create Hero'; 
export const CREATE_HERO_SUCCESS = '[Hero] Create Hero Success'; 
export const CREATE_HERO_FAIL = '[Hero] Create Hero Fail'; 
export const DELETE_HERO = '[Hero] Delete Hero'; 
export const DELETE_HERO_SUCCESS = '[Hero] Delete Hero Success'; 
export const DELETE_HERO_FAIL = '[Hero] Delete Hero Fail'; 
export const UPDATE_HERO = '[Hero] Update Hero'; 
export const UPDATE_HERO_SUCCESS = '[Hero] Update Hero Success'; 
export const UPDATE_HERO_FAIL = '[Hero] Update Heo Fail'; 





export class GetHeroesAction implements Action { 
      readonly type = GET_HEROES; 
      constructor(public payload: Hero) {} 
} 
export class GetHeroesSuccessAction implements Action { 
      readonly type = GET_HEROES_SUCCESS; 
      constructor(public payload: Hero) {} 
} 
export class GetHeroesFailAction implements Action { 
      readonly type = GET_HEROES_FAIL; 
      constructor(public payload: Hero) {} 
} 
export class GetHeroAction implements Action { 
      readonly type = GET_HERO; 
      constructor(public payload: Hero) {} 
} 
export class GetHeroSuccessAction implements Action { 
      readonly type = GET_HERO_SUCCESS; 
      constructor(public payload: Hero) {} 
} 
export class GetHeroFailAction implements Action { 
      readonly type = GET_HEROES_FAIL 
      constructor(public payload: Hero) {} 
} 
export class CreateHeroAction implements Action { 
      readonly type = CREATE_HERO; 
      constructor(public payload: Hero) {} 
} 
export class CreateHeroSuccessAction implements Action { 
      readonly type = CREATE_HERO_SUCCESS; 
      constructor(public payload: Hero) {} 
} 
export class CreateHeroFailAction implements Action { 
      readonly type = CREATE_HERO_FAIL; 
      constructor(public payload: Hero) {} 
} 
export class DeleteHeroAction implements Action { 
      readonly type = DELETE_HERO; 
      constructor(public payload: Hero) {} 
} 
export class DeleteHeroSuccessAction implements Action { 
      readonly type = DELETE_HERO_SUCCESS; 
      constructor(public payload: Hero) {} 
} 
export class DeleteHeroFailAction implements Action { 
      readonly type = DELETE_HERO_FAIL; 
      constructor(public payload: Hero) {} 
} 

export class UpdateHeroAction implements Action { 
      readonly type = UPDATE_HERO; 
      constructor(public payload: Hero) {} 
} 
export class UpdateHeroSuccessAction implements Action { 
      readonly type = UPDATE_HERO_SUCCESS; 
      constructor(public payload: Hero) {} 
} 
export class UpdateHeroFailAction implements Action { 
      readonly type = UPDATE_HERO_FAIL; 
      constructor(public payload: Hero) {} 
} 

export type HeroActions 
= GetHeroesAction 
| GetHeroesSuccessAction 
| GetHeroesFailAction 
| GetHeroAction 
| GetHeroSuccessAction 
| GetHeroFailAction 
| CreateHeroAction 
| CreateHeroSuccessAction 
| CreateHeroFailAction 
| DeleteHeroAction 
| DeleteHeroSuccessAction 
| DeleteHeroFailAction 
| UpdateHeroAction 
| UpdateHeroSuccessAction 
| UpdateHeroFailAction; 



    hero.effect.ts 

    import { Action } from '@ngrx/store'; 
    import { Injectable } from '@angular/core'; 
    import { Effect, Actions, toPayload } from '@ngrx/effects'; 
    import { Observable } from 'rxjs/Observable'; 
    import 'rxjs/add/operator/takeUntil'; 

    import 'rxjs/add/operator/map'; 
    import 'rxjs/add/operator/do'; 
    import { empty } from 'rxjs/observable/empty'; 

    // import other files 
    import { AppState } from './../reducers'; 
    import { Hero } from './../models/hero'; 
    import * as heroes from './../actions/hero.action'; 
    import { HeroService } from './../services/hero.service'; 

    import { GET_HEROES, GET_HEROES_SUCCESS, GET_HEROES_FAIL, GET_HERO, GET_HERO_SUCCESS, GET_HERO_FAIL} from './../actions/hero.action'; 
    import { CREATE_HERO, CREATE_HERO_SUCCESS, CREATE_HERO_FAIL, DELETE_HERO, DELETE_HERO_SUCCESS, DELETE_HERO_FAIL } from './../actions/hero.action'; 
    import { UPDATE_HERO, UPDATE_HERO_SUCCESS, UPDATE_HERO_FAIL } from './../actions/hero.action'; 



    @Injectable() 
    export class HeroEffects { 
      constructor(private actions$: Actions, private heroService: HeroService) {} 

    @Effect() getHeroes$: Observable<Action> = this.actions$ 
    .ofType(GET_HEROES) 
    .switchMap(action => 
     this.heroService.getHeroes() 
     .map(heroes => ({type: GET_HEROES_SUCCESS, payload: heroes})) 
     .catch(() => Observable.of({type: GET_HEROES_FAIL}))); 

    @Effect() getHero$: Observable<Action> = this.actions$ 
    .ofType(GET_HERO) 
    .switchMap(action => 
     this.heroService.getHero(name) 
     .map(hero => ({type: GET_HERO_SUCCESS, payload: hero})) 
     .catch(() => Observable.of({type: GET_HERO_FAIL}))); 

    @Effect() createHero$: Observable<Action> = this.actions$ 
    .ofType(CREATE_HERO) 
    .switchMap(action => 
     this.heroService.createHero(name) 
     .map(hero => ({type: CREATE_HERO_SUCCESS, payload: hero})) 
     .catch(() => Observable.of({type: CREATE_HERO_FAIL}))); 

    @Effect() deleteHero$: Observable<Action> = this.actions$ 
    .ofType(DELETE_HERO) 
    .switchMap(action => 
     this.heroService.deleteHero(name) 
     .map(hero => ({type: GET_HERO_SUCCESS, payload: hero})) 
     .catch(() => Observable.of({type: GET_HERO_FAIL}))); 
    @Effect() updateHero$: Observable<Action> = this.actions$ 
    .ofType(UPDATE_HERO) 
    .switchMap(action => 
     this.heroService.updateHero(name) 
     .map(hero => ({type: UPDATE_HERO_SUCCESS, payload: hero})) 
     .catch(() => Observable.of({type: UPDATE_HERO_FAIL}))); 


} 

hero-list.reducer.ts 

import * as heroes from './../actions/hero.action'; 
import { Hero } from './../models/hero'; 


export type HeroListState = Hero[]; 

const initialState: HeroListState = []; 

export function heroListReducer(state = initialState, action: heroes.HeroActions): HeroListState { 
    switch (action.type) { 
    case heroes.GET_HEROES_SUCCESS: 
     return this.action.GET_HEROES_SUCCESS; 
    case heroes.CREATE_HERO_SUCCESS: 
     return [...state, this.action.CREATE_HERO_SUCCESS]; 
    case heroes.UPDATE_HERO_SUCCESS: 
     return state.map((hero) => { 
     if (hero.id === action.payload.id) { 
      return Object.assign({}, action.payload); 
     } 
     return hero; 
     }); 
    case heroes.DELETE_HERO_SUCCESS: 
     return state.filter(hero => hero.id !== this.action.DELETE_HERO_SUCCESS); 
    default: 
     return state; 
    } 
} 

答えて

0
あなたの減速のために

switch (action.type) { 
    case heroes.GET_HEROES_SUCCESS: 
     return action.payload; // this should not be this.action.GET_HEROES_SUCCESS; 

また、あなたがNGRX V4にヒーローズのツアーを変換しようとして立ち往生している場合。このlinkを見てください。同じデモがあります。

このlinkをご覧ください。 ngrxには小さなデモがあり、ngrxには4つのリンクがあります。

+0

提案していただきありがとうございますが、私はレデューサーで問題を解決しましたが、ヒーローはまだ表示されません。以前はそのリンクを見たことがありますが、残念ながら一部のファイルは古くなっているようです。たとえば、hero.actionコードを上記のリンクの例に変更すると、ペイロードに次のエラーメッセージが表示されます。「タイプ '{タイプ:文字列;ペイロード:any;}'がタイプ 'アクション'に割り当てられません。 @RahulSingh – bp3290

+0

@ bp3290更新された別のリンク私は電話で私の机ではない明日再び確認します –

関連する問題