话不多说直接上代码
1.使用mobx需要下载mobx和mobx-react两个依赖包

使用mobx 创建前端store并使用

 

2.写一个store的类文件比如:

      import { observable, action } from 'mobx'
      class Mobx1 {
              @observable number;

               constructor() {
                     this.number = 0
                 }

              @action
               addNumber = (number) => {
                    this.number = number
               }

       }
      const MyMobx = new Mobx1();
      export { MyMobx };

3. 新建总的store文件,将store的类文件注入到总的store文件中
 
    import { MyMobx } from './mobx'
    const store = {
            MyMobx
     }

    export default store
 

4. 将总的store文件注入到路由的外层,也是页面的外层

使用mobx 创建前端store并使用

 

使用mobx 创建前端store并使用 

 
使用mobx 创建前端store并使用

 

 5. 在使用页面中需要先用inject注入使用的store类,在使用this.props下的这个store类的方法及属性

使用mobx 创建前端store并使用

使用mobx 创建前端store并使用

 

 

然后就能愉快的使用了哈哈 

 

相关文章:

  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2021-11-22
  • 2022-01-12
  • 2021-04-11
  • 2021-07-18
  • 2021-04-03
猜你喜欢
  • 2021-10-03
  • 2022-12-23
  • 2021-06-27
  • 2021-04-13
  • 2021-06-24
  • 2021-09-25
  • 2022-12-23
相关资源
相似解决方案