在行动中反应 Dispatcher.dispatch,而不是将有效负载分派到存储
Posted
技术标签:
【中文标题】在行动中反应 Dispatcher.dispatch,而不是将有效负载分派到存储【英文标题】:react Dispatcher.dispatch in action, not dispatching payload to store 【发布时间】:2016-12-03 16:34:46 【问题描述】:我在“action”中设置了有效负载,目的是将 api 数据发送到商店。但是在 Dispatcher.register 中不会触发 switch case。
助焊剂版本:“助焊剂”:“^2.1.1”,
1)动作文件:(注意:已确认receiveAllServices是使用调试器触发的)
"use strict"
var Dispatcher = require('../dispatcher/appDispatcher');
// var requestActions = require('./requestActions');
var ActionTypes = require('../constants/actionTypes');
var ResponseActions =
receivedAllServices: function(all_services)
console.log('response received');
debugger;
Dispatcher.dispatch(
actionType: ActionTypes.RECEIVED_ALL_SERVICES,
services: all_servicess
);
;
module.exports = ResponseActions;
2) 存储:(注意:存储操作中的调试器不会触发)
Dispatcher.register(function(action)
switch(action.actionType)
case ActionTypes.RECEIVED_ALL_SERVICES:
debugger;
// AuthorStore.emitChange();
break;
);
3) 调度程序文件:
var Dispatcher = require('flux').Dispatcher;
module.exports = new Dispatcher();
4) actionTypes.js 文件
"use strict"
var keyMirror = require('fbjs/lib/keyMirror');
module.exports = keyMirror(
RECEIVED_ALL_SERVICES: null,
);
【问题讨论】:
【参考方案1】:嗯,很奇怪。一切看起来都是正确的。你能检查store.js
和action.js
中的Dispatcher
是同一个对象吗?
请参阅下面具有相同操作/商店的简短示例。一切正常(按run
并查看控制台输出)。
var appDispatcher = new Flux.Dispatcher();
// This code should be our action
function receivedAllServices()
appDispatcher.dispatch(
actionType: 'RECEIVED_ALL_SERVICES',
services: ['serv1','serv2'],
);
console.log('Done');
// This code should be our store
appDispatcher.register(action =>
console.log('action', action);
switch(action.actionType)
case 'RECEIVED_ALL_SERVICES':
//debugger;
// AuthorStore.emitChange();
console.log('AuthorStore.emitChange');
break;
);
// Call the action
receivedAllServices();
<script src="https://cdnjs.cloudflare.com/ajax/libs/flux/2.1.1/Flux.js"></script>
【讨论】:
以上是关于在行动中反应 Dispatcher.dispatch,而不是将有效负载分派到存储的主要内容,如果未能解决你的问题,请参考以下文章