我之前在react中使用。env没有传递api-key的问题但现在情况有点棘手我需要像这样声明api-key
const instance = axios.create({
withCredentials: true,
baseURL: "https://social-network.samuraijs.com/api/1.0/",
headers: {
"API-KEY": "API-KEY-GO-HERE"
}
});
就是它的对象。我读了关于。env的内容,我知道我不能传递。env样式的对象。
当我尝试传递API-KEY时,它不起作用。这是我的Base.js文件
export const API_KEY = process.env.REACT_APP_API_KEY
这是我的api.js文件
import {API_KEY} from "./base";
const instance = axios.create({
withCredentials: true,
baseURL: "https://social-network.samuraijs.com/api/1.0/",
headers: {
"API-KEY": API_KEY
}
});
不要为我工作。
我怎样才能正确地传递。env使它工作?
问题只在重新启动react-app时出现。如果不重新启动,.env
文件中的变量不会更新。
这部分代码是工作示例
import {API_KEY} from "./base";
const instance = axios.create({
withCredentials: true,
baseURL: "https://social-network.samuraijs.com/api/1.0/",
headers: {
"API-KEY": API_KEY
}
});