Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集中式申明,管理异步请求接口 #2

Closed
1 task done
alex86gbk opened this issue Jun 29, 2018 · 1 comment
Closed
1 task done

集中式申明,管理异步请求接口 #2

alex86gbk opened this issue Jun 29, 2018 · 1 comment
Labels
enhancement New feature or request

Comments

@alex86gbk
Copy link
Owner

alex86gbk commented Jun 29, 2018

  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

在业务模块和组件中可以随意使用远程接口完成异步请求

What does the proposed API look like?

类似 Dva 中声明 modelsservices 并关联后使用 this.props.dispatch

@alex86gbk alex86gbk added the enhancement New feature or request label Jun 29, 2018
@alex86gbk
Copy link
Owner Author

  1. 声明请求 service
    路径:/src/services
    文件:indexServices.js(对应/src/pages/index.js业务模块)
    示例:
/**
 * 获取所有年级
 * @param params
 * @return {Promise.<Object>}
 */
export async function getGrades(params) {
  return request({
    url: '/api/common/getGrades',
    method: "POST",
    data: params.payload
  });
}
  1. 作为props传递给组件或在业务模块直接使用
    路径:/src/pages
    文件:index.js
    示例:
import ReactDOM from 'ReactDOM'
import List from '../components/List.jsx';
import style from './index.css';
import * as service from '../services/indexServices';

ReactDOM.render((
  <div className={style.hello}>
    <h1>Hello js-multi-seed!</h1>
    <List dispatch={service} />
  </div>
), document.getElementById('main'));
  1. 发起异步请求
    路径:/src/components
    文件:List.jsx
    示例:
/**
 * 获取年级数据
 */
getGradesData = () => {
  this.props.dispatch.getGrades({
    payload: {
      id: 123
    }
  }).then((data) => {
    if (data) {
      this.setState({
        data: data.returnEntity
      });
    }
  })
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant