我正在用山羊腿/丙氨酸氨基转移酶开发一个流量前端程序。我有问题触发存储更新与行动。听者并没有像我预期的那样发挥作用。
我正在处理的代码现在相当复杂。我将尝试将我的问题简化为以下代码。我希望它最终会记录"Hello!msg:一些信息“。显然,侦听器hello()甚至没有运行。
这是要运行的主要javascript (ES6)文件:
import alt from './alt';
class DummyActionsProto {
sayHello(msg) {
console.log("sayHello", msg);
return {msg}
}
}
var DummyActions = alt.createActions(DummyActionsProto);
class DummyStoreProto {
constructor() {
this.bindListeners({
hello: DummyActions.sayHello,
});
this.state = {
items: [],
};
}
hello(msg) {
console.log("Hello World! msg: "+msg);
}
}
var DummyStore = alt.createStore(DummyStoreProto, 'DummyStore');
// trigger the action
DummyActions.sayHello("Some message");它包含的普通alt.js代码包括:
import Alt from 'alt';
module.exports = new Alt();我的问题是什么?
发布于 2015-07-04 16:34:24
简而言之,存储只能在操作方法中添加this.dispatch()时才能捕获操作。因此,与其在该方法中返回任何内容,不如运行this.dispatch() (有参数还是不带参数)。侦听器将使用this.dispatch()的参数运行。
更正后的版本:
import alt from './alt';
class DummyActionsProto {
sayHello(msg) {
console.log("sayHello", msg);
this.dispatch(msg); // **instead of return, you should do this**
}
}
var DummyActions = alt.createActions(DummyActionsProto);
class DummyStoreProto {
constructor() {
this.bindListeners({
hello: DummyActions.sayHello,
});
this.state = {
items: [],
};
}
hello(msg) {
console.log("Hello World! msg: "+msg);
}
}
var DummyStore = alt.createStore(DummyStoreProto, 'DummyStore');
// trigger the action
DummyActions.sayHello("Some message");https://stackoverflow.com/questions/31218487
复制相似问题