请求服务

服务层负责处理发起的请求, 并返回对服务端的响应。

配置

请求地址配置

EPS

  • 开发环境下,service 数据储存于 localStorage
  • 生产环境下,service 数据储存于 build/cool/temp/eps.json 中,应用打包后会自动写入到配置中

WARNING

控制台出现 service 相关的报错,如,service.xxx is not a function 等问题时,请查看 network 中的 eps 接口是正常

编辑

  • 程序默认使用 eps 方式(即后端所有开放给 app 端的接口,都会在前端生成对应的方法),无需自己手动一个个添加:
// /cool/config.ts
const test = {
	// 是否开启
	eps: true
};
  • 当然也可以自己额外配置 service,在 service 目录下新建 test.ts 文件:
// /service/test.ts
import { BaseService, Service } from "/@/cool";

@Service("test")
class Test extends BaseService {}

export default Test;

上面两种方式最终会合并生成 service 及描述文件

使用

引入

// 方式1
import { useCool } from "/@/cool";
const { service } = useCool();

// 方式2
import { service } from "/@/cool";

调用

// 通过方法
service.test.page().then((res) => {
	console.log(res);
});

// 请求其他
service
	.request({
		url: "http://xxxx",
		method: "GET",
		data: {},
		params: {}
	})
	.then((res) => {
		console.log(res);
	});

service 的结构是根据 接口路径 转化成对应的层级,打印出 service 瞅瞅。

Last Updated: