【发布时间】:2017-05-26 08:51:21
【问题描述】:
我尝试做那个商店对象 UserStore.js
import { observable, action } from 'mobx';
class UserStore {
constructor() {
const me = observable({
me: null,
auth: action.bound(function(me) {
this.me = me;
})
})
}
}
export default UserStore;
在这之后,我会这样做
App.js
const App = inject('routing','UserStore')(observer(class App extends Component {
constructor(props, context) {
super(props, context);
this.handleFiles = this.handleFiles.bind(this);
this.prepareTable = this.prepareTable.bind(this);
this.state = {excel: null};
}
render() {
const {location, push, goBack} = this.props.routing;
const {userStore} = this.props.userStore;
在 index.js 中我会这样做
const stores = {
// Key can be whatever you want
routing: routingStore,
UserStores
};
const history = syncHistoryWithStore(browserHistory, routingStore);
ReactDOM.render(
<Provider {...stores}>
<Router history={history}>
<Entry/>
</Router>
</Provider>,
document.getElementById('root')
);
让我们尝试打开 localhost:3000 并查看此错误
错误:MobX 观察者:存储 'UserStore' 不可用!确保它是由某个 Provider 提供的
更新: 我正在使用 create-react-app 创建一个项目,但我不能在代码中使用 @(@injector 的示例)
【问题讨论】:
-
你写的是
UserStores而不是UserStore。还尝试导出 UserStore 的实例,而不是类本身:export default new UserStore();
标签: javascript reactjs mobx mobx-react