类组件使用mobx实现数据修改以及请求数据

src下创建新的共享数据文件test.js

import {observable, computed, action, autorun,runInAction, configure,makeAutoObservable} from 'mobx';
import Unit from "../unit/index";
configure({ enforceActions: 'observed' })//开启严格模式
// https://cn.mobx.js.org/best/actions.html#runinaction-%E5%B7%A5%E5%85%B7%E5%87%BD%E6%95%B0
class Store {
    @observable cls="web1908"
    constructor(){
        makeAutoObservable(this)
    }
    @observable resume={
        base:{
            name:"",
            age:"",
            sex:""
        },
        exp:{

        }
    }
    @action
    update=()=>{
        this.cls="web1908A";
        this.resume={
            base:{
                name:"张",
                age:"22",
                sex:"男"
            }
        }
    }
    @observable pddList=[]
    @action 
    getPdd=()=>{
        Unit.getApi2("/home/mediareports",{
            page_number:1,
            page_size:20
        },{}).then((res)=>{
            runInAction(()=>{
                this.pddList=res.data.data
            })
            //黄色感叹提示就没辽
        })
    }
}

export default Store;

router.js中配置

在这里插入图片描述

页面中具体操作

import React, { Component } from 'react';
import {NavLink,withRouter} from "react-router-dom"
import {observer,inject} from 'mobx-react';
@withRouter
@inject('Test')
@observer
class index extends Component {
    constructor(props){
        super(props)
        this.state={
            obj:{
                a:1,
                b:2
            }
        }
    }
    heBing(){
        const {obj}=this.state
        const newobj = {
            a:2
        }
        const obj2 = {
            ...obj,
            ...newobj
        }
        this.setState({
            obj:obj2
        })
        console.log(obj)

    }
    lists(){
        const {pddList}=this.props.Test
        return pddList.map((item,index)=>{
            return <p key={index}>{item.main_title}</p>
        })
    }
    componentDidMount(){
        const {getPdd}=this.props.Test
        getPdd()
        this.lists()
    }
    render() {
    const {cls,resume,update,pddList}=this.props.Test
    const {obj}=this.state

        return (
            <div>
                <div>
                    
                </div>
                你的班级是:{cls}
                <br />
                您的姓名:{resume.base.name}
                <br />
                您的年龄:{resume.base.age}
                <br />
                您的性别:{resume.base.sex}
                <br />
                <input type="button" value="更新信息" onClick={()=>{update()}} />
                <br />
                {obj.a}---{obj.b}
                <br />
                <input type="button" value="修改a" onClick={()=>{this.heBing()}} />
                <br />
                {
                pddList.length>0&&this.lists()
                }
                
            </div>
        );
    }
}

export default index;


  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上流星&洒下星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值