简体   繁体   English

@ ngrx /使用Angular 2进行存储:无法读取未定义的属性

[英]@ngrx/store with Angular 2: Cannot read property of undefined

I'm trying to learn @ngrx/store with Angular 2 - RC4. 我正在尝试使用Angular 2-RC4学习@ ngrx / store。 I've got a service that I'm calling from my component, and I'm just trying to console log a list of vehicles whenever it changes. 我有一个从组件调用的服务,而我只是想在发生变化时尝试控制台记录车辆列表。

Vehicle 车辆

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

Vehicle Reducer 车辆减速器

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 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: {}
    })
  ]
);

VehiclesService VehiclesService

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));
  }
}

AppComponent AppComponent

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));
  }
}

But when it runs, I get a TypeError TypeError: Cannot read property 'vehicles' of undefined 但是当它运行时,我得到一个TypeError TypeError: Cannot read property 'vehicles' of undefined

The first console.log returns a Store object, but the subscription seems to fail. 第一个console.log返回一个Store对象,但是订阅似乎失败了。

Any idea what I'm doing wrong? 知道我在做什么错吗?

您需要将provideStore更改为provideStore({vehicles: vehicles})

In my case, the error was the use of different names in the reducer object and the interface: 就我而言,错误是在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;
}

There is not enough information: template app.component.ts is missing in the question. 没有足够的信息:问题中缺少模板app.component.ts

However, check how do you access the member vehicles in your template. 但是,请检查如何访问模板中的成员vehicles Try operator ?. 尝试运算符?. instead of . 代替. . Something like: 就像是:

{{ yourObject?.vehicles }}

声明商店时,我认为正确的方法是:

constructor(private http: Http, private store: Store<VehicleState>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM