zoukankan      html  css  js  c++  java
  • 使用mobx 创建前端store并使用

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

    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文件注入到路由的外层,也是页面的外层

     

     

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

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

  • 相关阅读:
    Windows ETW 学习与使用三
    暗云Ⅳ对SATA磁盘MBR Hook探索
    msahci代码调试备份
    mimikatz使用命令记录
    Windows ETW 学习与使用一
    RabbitMQ 实现延迟队列
    Redis 脱坑指南
    浅析 ThreadLocal
    IDEA2020.2.3破解
    用友NC 模块 简写(瞎猜的)
  • 原文地址:https://www.cnblogs.com/cnlg123/p/13729970.html
Copyright © 2011-2022 走看看