2017-06-22 132 views
1

我想在React項目中使用Mobx(使用Meteor)並使用mobx-react創建全局通知存儲。Mobx返回空對象

我嘗試初始化模塊,但我不知道爲什麼它返回一個空對象:

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 { } 

任何人都知道爲什麼我的對象是空的 ?

謝謝社區!

+0

這很奇怪。它工作[**爲我**](http://jsbin.com/cenibayuyi/1/edit?js,console,output)。也許你會在代碼中的其他地方覆蓋'window.store'? – Tholle

+0

嗨!在你的例子中,空的對象也是,nop? –

+0

寫'store.notifications.toJS()',你會看到值。 – Tholle

回答

1

我認爲它按預期工作。嘗試在this console中寫入store.notifications.toJS()以查看您的陣列。請記住,MobX陣列不是真正的JavaScript數組,因此在爲外部庫記錄或傳遞值以使其正常工作時,可能必須使用toJS