Javascript 未注入Mobx存储
我试着做那件事 UserStore.jsJavascript 未注入Mobx存储,javascript,reactjs,mobx,mobx-react,Javascript,Reactjs,Mobx,Mobx React,我试着做那件事 UserStore.js import { observable, action } from 'mobx'; class UserStore { constructor() { const me = observable({ me: null, auth: action.bound(function(me) { this.me = me; })
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')
);
const存储={
//钥匙可以是你想要的任何东西
路由:路由存储,
用户商店
};
const history=syncHistoryWithStore(浏览器历史记录、路由存储);
ReactDOM.render(
,
document.getElementById('root'))
);
在所有这些之后,让我们尝试打开localhost:3000并消除这个错误
错误:MobX观察员:存储“UserStore”不可用!确保它是由某个提供商提供的
更新:
我正在用create-react-app创建一个项目,我不能在代码中使用@in(例如@injector)我想你应该返回一个商店实例。 为了更有条理,我有一个类似“storeInitializer.ts”的文件(这里使用typescript): 然后我有另一个类似“storeIdentifier.ts”的文件: 在应用程序文件中,我执行以下操作:
import React from 'react';
import { inject, observer } from 'mobx-react';
import Stores from './storeIdentifier';
const App = inject(Stores.YourStoreName)(observer(props: any) => {
//code here
});
export default App;
或者,如果您使用的是类组件方法
import React from 'react';
import { inject, observer } from 'mobx-react';
@inject(Stores.YourStoreName)
@observable
class App extends Component {
//code here
}
export default App;
我想你应该返回一个商店的实例。 为了更有条理,我有一个类似“storeInitializer.ts”的文件(这里使用typescript): 然后我有另一个类似“storeIdentifier.ts”的文件: 在应用程序文件中,我执行以下操作:
import React from 'react';
import { inject, observer } from 'mobx-react';
import Stores from './storeIdentifier';
const App = inject(Stores.YourStoreName)(observer(props: any) => {
//code here
});
export default App;
或者,如果您使用的是类组件方法
import React from 'react';
import { inject, observer } from 'mobx-react';
@inject(Stores.YourStoreName)
@observable
class App extends Component {
//code here
}
export default App;
您编写了
UserStores
,而不是UserStore
。还要尝试导出UserStore的实例,而不是类本身:export default new UserStore()
您编写了UserStores
而不是UserStore
。还要尝试导出UserStore的实例,而不是类本身:export default new UserStore()代码>