react-native - Expo React Native 项目的 Axios 配置中未设置环境变量
问题描述
我正在尝试.env
在我的 Expo 项目根目录中使用一个文件,该文件带有一个用于 Axios 请求的 API URL,位于一个名为axios.ts
.
import axios from 'axios';
const Axios = axios.create({
baseURL: process.env.REACT_APP_API_URL
});
Axios.defaults.headers.common['Content-Type'] = 'application/json';
export default Axios;
为了使其正常工作,我按照Expo 官方文档的dot-env
建议安装了该库
这是我的package.json
{
"main": "index.js",
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"start-expo": "expo start",
"test": "jest"
},
"dependencies": {
"@react-native-community/masked-view": "^0.1.10",
"@react-native-community/picker": "1.6.0",
"@react-navigation/drawer": "^5.9.0",
"@react-navigation/material-top-tabs": "^5.2.13",
"@react-navigation/native": "^5.7.0",
"@react-navigation/native-stack": "^5.0.5",
"@react-navigation/stack": "^5.9.0",
"axios": "^0.20.0",
"dotenv": "^8.2.0",
"events": "^3.1.0",
"react": "~16.11.0",
"react-dom": "~16.11.0",
"react-native": "~0.62.2",
"react-native-gesture-handler": "~1.6.0",
"react-native-paper": "^3.10.1",
"react-native-reanimated": "~1.9.0",
"react-native-screens": "~2.9.0",
"react-native-tab-view": "^2.14.4",
"react-native-table-component": "^1.2.1",
"react-native-unimodules": "~0.10.0",
"react-native-vector-icons": "^7.0.0",
"react-native-web": "~0.11.7",
"react-native-webview": "9.4.0",
"react-redux": "^7.2.0",
"redux-saga": "^1.1.3"
},
"devDependencies": {
"@babel/core": "~7.9.0",
"@types/react": "~16.9.23",
"@types/react-dom": "~16.9.8",
"@types/react-native": "~0.61.23",
"@types/react-native-vector-icons": "^6.4.5",
"babel-plugin-inline-dotenv": "^1.6.0",
"babel-preset-expo": "~8.2.0",
"expo": "~38.0.1",
"expo-updates": "~0.2.8",
"jest-expo": "~38.0.0",
"typescript": "~4.0.2"
},
"jest": {
"preset": "react-native"
},
"private": true
}
当然我也设置了babel.config.js
文件
module.exports = function(api) {
api.cache(true);
return {
plugins: ['inline-dotenv'],
presets: ['babel-preset-expo'],
};
};
当我对某个端点进行 API 调用时,就会出现问题。
import Axios from "../../api/axios";
const login = async () => {
console.log(process.env.REACT_APP_API_URL);
try{
const response = await Axios.post(ENDPOINTS.login,{
email,
password,
device_id
});
console.log(response);
}catch(e){ //TODO : deal with errors
console.log(e);
}
};
404 error
如果我直接设置字符串,则在使用.env
文件并正常工作时请求会得到一个。
import axios from 'axios';
const Axios = axios.create({
baseURL: 'http://myurl..." // This is working as expected!
});
Axios.defaults.headers.common['Content-Type'] = 'application/json';
export default Axios;
似乎.env
文件设置不正确,但根据console.log()
,它具有正确的值。我怀疑原因是环境变量是在调用 Axios 函数之后设置的,所以一开始它是空的,但是当日志出现时它已经设置好了。
我的解释是否合理或其他设置可能导致此问题?
解决方案
第二天测试后,我很惊讶它工作正常,所以我假设缓存有问题。对于遇到此问题的任何人,我建议删除 Expo 中的缓存并重试
推荐阅读
- python - 条形图上的自定义 xticks 标签(matplotlib)
- sql - 如何编写一个 sql 语句,为日期值创建一个新列,然后在 WHERE 子句中查询它
- pandas - 在不基于日期的列值之前根据日期选择行
- data-virtualization - 没有缓存的不同源之间的 Denodo 连接
- c++ - 如何创建表格?
- aws-api-gateway - 在 API 网关下多次获取单个资源的方法?
- templates - 搜索过滤器在网站 Odoo 12 中不起作用
- javascript - 如何通过索引或 JavaScript 中的任何其他可能方式访问对象内部存在的数组元素
- python - How do I verify that multiple users fulfill the requirements of their input?
- c# - EDI.Net 列表限制为 50 个