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

有没有请求的例子 #25

Closed
liminjian1437 opened this issue Dec 15, 2021 · 8 comments
Closed

有没有请求的例子 #25

liminjian1437 opened this issue Dec 15, 2021 · 8 comments

Comments

@liminjian1437
Copy link

0.0 你这种写法 我看了半天 不知道请求要怎么写了

@honghuangdc
Copy link
Member

honghuangdc commented Dec 15, 2021

在 service/api/demo 里面有两个示例:fetchDictionary、fetchDictionaryWithMiddleware
在页面上引入这两个函数
import { fetchDictionary, fetchDictionaryWithMiddleware } from '@/service;

async function testApi() {
const { data: data1 } = await fetchDictionary("管理熵");
if(data1) {
console.log(data1);
}
const { data: data2 } = await fetchDictionaryWithMiddleware("管理熵");
if(data2) {
console.log(data2);
}
}

testApi();

//按照上面的调用一下

@liminjian1437
Copy link
Author

666 感谢🙏

@honghuangdc
Copy link
Member

请求函数前面加个 await

@liminjian1437
Copy link
Author

牛啊~ 可以了。我好好学习 下你的代码

@liminjian1437
Copy link
Author

请求函数前面加个await

大佬还有问题想请教下,如果有多个请求地址应该怎么改呢,还有怎么加请求代理呢。

@honghuangdc
Copy link
Member

  1. 在env.development 和 env.production 和env.staging里面添加一个新的请求地址
    例如:VITE_HTTP_BAIDU_URL=https://www.baidu.com
    2.在src/typings/env.d.ts 添加VITE_HTTP_BAIDU_URL: string;
  2. 在src/service/request/index.ts 创建另一个request,
    例如:
    export const baiduRequest = createRequest({
    baseURL: import.meta.env.VITE_HTTP_BAIDU_URL,
    timeout: REQUEST_TIMEOUT
    });
  3. 再去api文件夹下创建对应业务的文件,引入baiduRequest即可

@honghuangdc
Copy link
Member

代理的话,需要在vite.config.ts添加配置,参照文档:https://cn.vitejs.dev/config/#server-proxy

@liminjian1437
Copy link
Author

  1. 在env.development 和env.production 和env.staging里面添加一个新的请求地址
    例如:VITE_HTTP_BAIDU_URL= https://www.baidu.com
    2.在src/typings/env.d.ts 添加VITE_HTTP_BAIDU_URL: string;
  2. 在src/service/request/index.ts创建另一个请求,
    例如:
    export const baiduRequest = createRequest({
    baseURL: import.meta.env.VITE_HTTP_BAIDU_URL,
    timeout: REQUEST_TIMEOUT
    });
  3. 再去api文件夹下创建对应业务的文件,引入百度请求发酵

谢谢,说的很详细,看明白了。哈哈

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

No branches or pull requests

2 participants