ant design pro 初識


發送請求

上次講到在api.js中發送請求,模擬了假數據,這次講一下調用真實接口進行請求並渲染頁面。

先完整的過一遍請求吧

首先view層發送請求例如下面的代碼:

 componentDidMount() {
    this.fetchListCallback();
  }

  fetchListCallback = () => {
    const { limit, offset } = this.state;
    const { dispatch } = this.props;
    dispatch({
      type: 'brandDiscountManage/fetchList',
      payload: {
        limit,
        offset,
      },
      callback: (response)=>{
        this.setState({
          tableData: common(response.data.rows, 'discountId'),
        })
      },
    })
  };

這里調用了'brandDiscountManage'命名空間下的 fetchList 方法。
另外需要在路由層配置一下,引用的文件。

'/brandDiscountManage': {
  component: dynamicWrapper(app, ['brandDiscountManage'], () => import('../routes/brand/DiscountManage/index')),
},

如上代碼顯示,這里用到了brandDiscountManage.js。其中的文件內容如下
(effects里面就是用到的一些處理請求方法)

import { responseErrorMsg } from '../assets/common'
import { getDiscountList } from '../services/api';

export default {

  namespace:'brandDiscountManage',

  state:{

  },

  effects: {
    *fetchList({payload, callback}, {call}) {
      const response = yield call(getDiscountList, payload)
      if(responseErrorMsg(response) && callback ){
        callback(response);
      }
    },
  },

  reducers: {

  },
}

這樣在看上面的代碼就知道,頁面一進來就調用了fetchList方法。這個方法里面調用了yield call (函數名, payload參數)這個方法(暫時不太懂實現原理,一筆帶過),並把響應值response 返回。然后判斷有沒有錯誤,有的話 responseErrorMsg 這個函數里面做了處理,沒有錯誤的話並且存在callback就會調用callback,並把response作為參數傳送。
接着看getDiscountList這個函數函數里面做了什么,這個函數存在於'../services/api.js'文件夾里面

看代碼

export async function  getDiscountList(params) {
  return request('/jiuyue/discount/discountList', {
    method: 'POST',
    body: {
      sort: 'create_dts',
      order: 'DESC',
      ...params,
    },
  });
}

這里調用的是真實借口,話說真實的借口應該是域名+地址,這里面只有地址,別急,這里面用到了代理。實際上,在剛開始做的時候我直接把網址放到這個request后面,發現上個公司的測試環境的請求地址http:....是可以訪問的,然后當我把現在公司的開發環境的全部地址放上去時候報錯了,找不到這個借口,我不知道怎么回事。最終解決方案是用到了上面說的代理。ant design pro用到了webpack環境可以配置代理。
具體是在 .webpackrc.js 文件中,
具體代碼如下

const path = require('path');

export default {
  entry: 'src/index.js',
  extraBabelPlugins: [
    ['import', { libraryName: 'antd', libraryDirectory: 'es', style: true }],
  ],
  env: {
    //開發環境
    development: {
      extraBabelPlugins: ['dva-hmr'],
    },
    // build 時的生產環境
    production: {
      'extraBabelPlugins': [
        'transform-runtime',
        'transform-decorators-legacy',
        ['import', { 'libraryName': 'antd', 'style': true }],
      ],
    },
  },
  //設置代理請求
  proxy: {
    '/jiuyue': {
      "target": "http://jiuyue.raykoon.com",
      "changeOrigin": true,
      "pathRewrite": { "^/jiuyue" : "" }
    }
  },
  alias: {
    components: path.resolve(__dirname, 'src/components/'),
  },
  ignoreMomentLocale: true,
  theme: './src/theme.js',
  html: {
    template: './src/index.ejs',
  },
  //
  disableDynamicImport: true,
  //如果你的靜態資源域名和應用域名不同(例如獨立的 cdn 地址),你需要在 .webpackrc 文件里用 publicPath 對生產環境的靜態路徑進行配置。
  publicPath: '/',
  hash: true,
};

上面就是我的源文件,相信也沒有什么值得屏蔽的,代理主要用的 proxy 這個屬性的配置。
如果你剛才細心的話就會發現我剛才request后面的借口前面有一個 /jiueyu 這個前綴,在這個代理這里做了處理,它的大致意思就是說遇到帶有 /jiuyue的網址,就作處理,域名重定向到 http://jiuyue.raykoon.com 這個域名下,並且把 /jiuyue前面全部替換成""


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM