我想在 React 项目(使用 Meteor)中使用 Mobx 并使用 mobx-react
创建全局通知存储。
我尝试初始化模块,但我不知道为什么它返回一个空对象:
import { observable } from 'mobx';
class NotificationStore {
@observable notifications = ['test', 'new notification'];
}
var store = window.store = new NotificationStore;
export default store;
在我的 App.js 中:
import store from '../../components/NotificationStore';
console.log(store); // return Object { }
有人知道为什么我的对象是空的吗?
感谢社区!
最佳答案
我认为它正在按预期工作。尝试在 this console 中写入 store.notifications.toJS()
查看你的数组。请记住,MobX 数组不是真正的 JavaScript 数组,因此在记录或将值传递到外部库时您可能必须使用 toJS
才能使其正常工作。
关于javascript - Mobx 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700301/