我的商店有一个奇怪的问题,我正在尝试获取数据并将其存储在商店中,但是由于某种原因,无论我尝试多少,数据都不会持久。这是我的代码。
class FactoryStore extends EventEmitter {
constructor() {
super();
this._factories = [];
fetch('/factories').then(results => {
return results.json();
}).then(data => {
this._setFactories(data);
console.log(data); // <------------------- Prints the data as expected
}).catch(error => console.log(error));
console.log(this.getFactories()); // <------------ shows nothing, it's empty
}
_setFactories(factories) {
this._factories = factories;
}
getFactories() {
return this._factories;
}
}
另外!我找到了另一个答案。谢谢大家将其标记为重复!