一开始傻傻地在router下面使用provide,
class App extends Component {
render() {
return (
<div>
<Router>
<Switch>
<Route exact path="/" > <Home /></Route>
<Route path="/cart" > <Cart /></Route>
<Route path="/my" ><My /></Route>
</Switch>
</Router>
<Provider cartStore={cartStore}>
<Cart />
</Provider>
</div>
)
}
}
当然会报错:
Error: MobX injector: Store 'cartStore' is not available! Make sure it is provided by some Provider
在路由中正确滴使用应该是这样的👇