看看仓库https://github.com/mappedinn/ng-ngrx-entity-pizza-firebase-effects:
npm i
这是我的减速器的定义方式:
export function reducer(state = initialState, action: PizzaActions): PizzaState {
switch (action.type) {
case PizzaActionTypes.PIZZA_ADDED:
// if there is one pizza added in the backend, so the store has to be added
console.log('action=');
console.log(JSON.stringify(action));
console.log('action.payload=');
console.log(JSON.stringify(action.paylaod));
console.log(JSON.stringify(action['paylaod']));
// return pizzaAdaptor.addOne({id: '003', size: 'medium', status: 'cooking'}, state);
return pizzaAdaptor.addOne(action.paylaod, state);
case PizzaActionTypes.PIZZA_MODIFED:
// if there is one pizza being modified in the backend, so the store has to modify it
return pizzaAdaptor.updateOne({
id: action.paylaod.id,
changes: action.paylaod
}, state);
case PizzaActionTypes.PIZZA_REMOVED:
// if there is one pizza being removed in the backend, so the store has to remove it
return pizzaAdaptor.removeOne(action.paylaod.id, state);
default:
return state;
}
}
我在减少比萨饼减量器时遇到以下错误:
core.js:1624错误TypeError:无法读取未定义的属性“ id” 在selectId(http://localhost:4200/app-modules-pizza-pizza-module.js:424:94)处 在addOneMutably(http://localhost:4200/app-modules-pizza-pizza-module.js:124:19) at Object.operation [as addOne](http://localhost:4200/app-modules-pizza-pizza-module.js:101:25) 在减速器(http://localhost:4200/app-modules-pizza-pizza-module.js:873:33) 在http://localhost:4200/vendor.js:77764:20 结合使用(http://localhost:4200/vendor.js:77722:35 在computeNextEntry(http://localhost:4200/vendor.js:77058:21) 在recomputeStates(http://localhost:4200/vendor.js:77089:15) 在http://localhost:4200/vendor.js:77316:30 在ScanSubscriber.StoreDevtools.liftedAction $ .pipe.Object.state [作为累加器](http://localhost:4200/vendor.js:77381:38)
这使我认为操作可能未定义。 console.log(JSON.stringify(action))
支票显示如下:
action =
{“类型”:“已添加[披萨]”,“有效载荷”:{“ id”:“ 001”,“大小”:“大”,“状态”:“烹饪”}}
由于reducer通过pizzaAdaptor.addOne(action.paylaod, state);
向商店添加了新实体,因此我也通过action.payload
和console.log(JSON.stringify(action.paylaod));
检查了console.log(JSON.stringify(action['paylaod']));
。结果如下:
action.payload =
未定义
action ['有效载荷'] =
未定义
Reducer中的动作对象行为奇怪。
PS:如果我按如下所示手动设置action.payload
对象return pizzaAdaptor.addOne({id: '003', size: 'medium', status: 'cooking'}, state);
$ npm -v
6.1.0
$ node -v
v8.11.2
$ uname -a
# Linux X580VD 4.15.0-24-generic #26-Ubuntu SMP Wed Jun 13 08:44:47 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ npm ls --depth 0
ng-ngrx-entity-pizza-firebase-effects@0.0.0 /home/amine/docker-projects/ng-ngrx-entity-pizza-firebase-effects
├── @angular-devkit/build-angular@0.6.8
├── @angular/animations@6.0.7
├── @angular/cli@6.0.8
├── @angular/common@6.0.7
├── @angular/compiler@6.0.7
├── @angular/compiler-cli@6.0.7
├── @angular/core@6.0.7
├── @angular/forms@6.0.7
├── @angular/http@6.0.7
├── @angular/language-service@6.0.7
├── @angular/platform-browser@6.0.7
├── @angular/platform-browser-dynamic@6.0.7
├── @angular/router@6.0.7
├── @ngrx/effects@6.0.1
├── @ngrx/entity@6.0.1
├── @ngrx/schematics@6.0.1
├── @ngrx/store@6.0.1
├── @ngrx/store-devtools@6.0.1
├── @types/jasmine@2.8.8
├── @types/jasminewd2@2.0.3
├── @types/node@8.9.5
├── angularfire2@5.0.0-rc.11
├── codelyzer@4.2.1
├── core-js@2.5.7
├── firebase@5.2.0
├── jasmine-core@2.99.1
├── jasmine-spec-reporter@4.2.1
├── karma@1.7.1
├── karma-chrome-launcher@2.2.0
├── karma-coverage-istanbul-reporter@2.0.1
├── karma-jasmine@1.1.2
├── karma-jasmine-html-reporter@0.2.2
├── protractor@5.3.2
├── rxjs@6.2.1
├── ts-node@5.0.1
├── tslint@5.9.1
├── typescript@2.7.2
└── zone.js@0.8.26
获得的错误类似于https://github.com/ngrx/platform/issues/645#issuecomment-351787150
我用来制作披萨减少器的教程是https://www.youtube.com/watch?v=rv37jBygQ2g&list=PL0vfts4VzfNjrI4e_cJmqfSrUvaBADMlD&index=5
非常感谢您帮助我解决问题。