2

我正在尝试使用 Angular 2 - RC4 学习 @ngrx/store。我有一个从我的组件调用的服务,我只是想在它发生变化时控制台记录一个车辆列表。

车辆

export interface Vehicle {
  ...stuff...
}

车辆减速机

import { Vehicle } from '../models';

export interface VehiclesState {
  vins: string[];
  vehicles: { [vin: string]: Vehicle };
}

const initialState: VehiclesState = {
  vins: [],
  vehicles: {}
}

export const vehicles = (state: any = initialState, {type, payload}) => {
  switch (type) {
    case 'LOAD_VEHICLES':
      const vehicles: Vehicle[] = payload;
      const newVehicles = vehicles.filter(vehicle => !state.vehicles[vehicle.vin]);

      const newVehicleVins = newVehicles.map(vehicle => vehicle.vin);
      const newVehiclesList = newVehicles.reduce((vehicles: { [vin: string]: Vehicle }, vehicle: Vehicle) => {
        return mergeObjects(vehicles, {
          [vehicle.vin]: vehicle
        });
      }, {});

      return {
        vins: [...state.vins, ...newVehicleVins],
        vehicles: mergeObjects({}, state.vehicles, newVehiclesList)
      }
  }
}

main.ts

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { provideStore } from '@ngrx/store'

import { AppComponent, environment } from './app/';
import { vehicles } from './app/shared/reducers/vehicles'

if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent,
  [
    HTTP_PROVIDERS,
    provideStore(vehicles, {
      vins: [],
      vehicles: {}
    })
  ]
);

车辆服务

import {Http, Headers} from '@angular/http';
import {Injectable} from '@angular/core';
import {Store} from '@ngrx/store';
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';

import {Vehicle} from '../models/vehicle.model';

const BASE_URL = 'http://localhost:3000/vehicles/';
const HEADER = { headers: new Headers({ 'Content-Type': 'application/json' }) };

@Injectable()
export class VehiclesService {
  vehicles: Observable<Array<Vehicle>>;

  constructor(private http: Http, private store: Store<Vehicle[]>) {
    this.vehicles = this.store.select('vehicles');
  }

  loadVehicles() {
    this.http.get(BASE_URL)
      .map(res => res.json())
      .map(payload => ({ type: 'LOAD_VEHICLES', payload: payload }))
      .subscribe(action => this.store.dispatch(action));
  }
}

应用组件

import { Component, OnInit, Input } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { Store } from '@ngrx/store';

import { Vehicle, VehiclesService } from './shared';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  providers: [VehiclesService]
})
export class AppComponent implements OnInit{
  title = 'app works!';

  vehicles: Observable<Vehicle[]>;

  constructor(private vehiclesService: VehiclesService) {
    this.vehicles = vehiclesService.vehicles;

    vehiclesService.loadVehicles();
  }

  ngOnInit() {
    console.log(this.vehicles)
    this.vehicles.subscribe(vehicles => console.log(vehicles));
  }
}

但是当它运行时,我得到一个 TypeErrorTypeError: Cannot read property 'vehicles' of undefined

第一个 console.log 返回一个 Store 对象,但订阅似乎失败了。

知道我做错了什么吗?

4

4 回答 4

2

你需要改变你provideStore的存在provideStore({vehicles: vehicles})

于 2016-08-18T20:01:53.320 回答
1

就我而言,错误是在 reducer 对象和接口中使用了不同的名称:

import { LeaderboardParticipant } from './...';
import * as fromLeaderboard from './...';

export interface IState {
    leaderboard: fromLeaderboard.IState;
}

export const reducers = {
    leaderboard: fromLeaderboard.reducer // I used to have 'search' instead of 'leaderboard'
};

export function selectChallengeId(state: IState): string {
    return state.leaderboard.challengeId;
}

export function selectFilterTerms(state: IState): string {
    return state.leaderboard.filterTerms;
}
于 2017-06-28T23:26:35.817 回答
0

当你声明你的Store(商店)时,我认为正确的做法是:

constructor(private http: Http, private store: Store<VehicleState>)
于 2017-08-26T03:35:23.313 回答
0

信息不足:app.component.ts问题中缺少模板。

但是,请检查您如何访问vehicles模板中的成员。尝试运算符?.而不是.. 就像是:

{{ yourObject?.vehicles }}
于 2016-08-18T18:15:33.453 回答