我在“action”中设置了有效负载,目的是将api数据发送到商店。但是在Dispatcher.register中没有触发switch case。
Flux版本:“flux”:“^ 2.1.1”,
1)动作文件:(注意:已经确认使用调试器触发了receivedAllServices)
"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,
});
答案 0 :(得分:0)
Dispatcher
和store.js
中的action.js
是否是同一个对象?
请参阅以下具有相同操作/存储的简短示例。一切正常(按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>