通过Dva中的model、Reducer、Effect、Generator在项目中的实现

in 网站建设
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

View视图层文件:UserPage.jsx

import React, { Component } from 'react';
import UserUI from '../components/userUI'
import {connect} from 'dva'
// import {reqCnode} from '../services/example'
class UserPage extends Component{
    handlerBack = () =>{
        console.log(this.props);
        this.props.history.push('/')
    }
    handlerChangeName = () =>{
        console.log(this.props);
        this.props.dispatch({
            type:'IndexText/setName',
            payLoad:{
                name:'柠檬啊'
            }
        })
    }
    handlerGetData = () =>{
        this.props.dispatch({
            type:'IndexText/reqCnodeFn'
        })
    }
    componentDidMount(){
        // reqCnode().then(res=>console.log('reqCnode请求结果',res))
    }
    render(){
        console.log(this.props.state)
        const {name,cNodeData} = this.props.state
        return(
            <div>
                我是user页
                <button onClick={this.handlerBack}>回首页</button>
                <UserUI></UserUI>
                <div>
                   {this.props.msg} <br/>
                    {name}
                </div>
                <button onClick={this.handlerChangeName}>更改名字</button>
                <button onClick={this.handlerGetData}>发请求获取参数</button>
                {
                    cNodeData.map(item=>(
                        <div key={item.id}>
                            {item.title}
                        </div>
                    ))
                }
            </div>
        )
    }
}
const mapStateToProps = (state) =>{
    console.log('123',state)
    return{
        msg:'abc',
        state:state.IndexText
    }
}

export default connect(mapStateToProps)(UserPage);

Model文件 IndexText.js

import {reqCnode} from '../services/example'
export default{
    namespace:'IndexText',
    state:{
        name:'西柚',
        cNodeData:[]
    },
    reducers:{
        setName(state,payLoad){
            let _state = JSON.parse(JSON.stringify(state));
            _state.name = payLoad.payLoad.name;
            return _state;
        },
        setCnodeData(state,payLoad){
            let _state = JSON.parse(JSON.stringify(state));
            _state.cNodeData = payLoad.data
            return _state;
        }
    },
    effects:{
        *reqCnodeFn({payLoad},{put,call}){
            const result = yield call(reqCnode);
            if(result.data){
                yield put({
                    type:'setCnodeData',
                    data:result.data.data
                })
            }
        }
    }
}

数据请求 example.js

import request from '../utils/request';
const proxy = '/apis'

export function query() {
  return request('/api/users');
}

export function reqCnode(){
  return request(proxy + '/api/v1/topics');
}

配置代理 .webpackrc

{
    "proxy":{
        "/apis":{
            "target":"https://cnodejs.org",
            "changeOrigin":true,
            "pathRewrite":{
                "^/apis":""
            }
        }
    }
}


觉得对自己有帮助就点个赞

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看