UNPKG

@aliretail/react-utils

Version:
66 lines (59 loc) 1.26 kB
--- title: 接口配置 order: 2 --- ### 使用 ````ts /** * @alife/apimap 类似的 apimap 实现,用于项目开发中本地开发和实际线上使用 * * 用法: */ const apiMap = new ApiMap({ base: { // 给不带 host url 添加的 host,一般不需要设置 _HOST: '', apiA: 'default-url-of-apiA', apiB: 'default-url-of-apiB' }, local: { apiA: 'local-mock-of-apiA' }, development: { // custom api-url-map of development env }, pre: { // custom api-url-map of pre env }, production: { // custom api-url-map of production env } }); // 不同环境可输出不同的 url console.log(apiMap.getUrl('apiA')); ```` ````jsx import ReactDOM from "react-dom" import { epochUtils } from '@aliretail/react-utils'; const { initApiMap, getApiUrl } = epochUtils; function Demo(){ initApiMap({ base: { getUser: 'user/get', }, pre: { _HOST: 'https://pre.abc.taobao.com/api/' }, production: { _HOST: 'https://pre.abc.taobao.com/api/' }, }); return ( <div> <p> 获取getUser的api地址 </p> <div>{getApiUrl('getUser')}</div> </div> ); } ReactDOM.render(<Demo />, mountNode); ````