我想摆脱所有与 Axios 一起使用的硬编码端点。所以我想将它们替换为资源文件。
我创建了一个 constants.js 文件,并将该文件导入到我的 Vue 组件中。当我引用此文件中的值时,我看到 constants.js 文件中的字符串值未定义。
//这是constants.js文件
const endpoint_constants = {
COMPANIES_ENDPOINT: "users/companies",
SERVICES_ENDPOINT: "services",
SERVICES_TYPE_ENDPOINT: "services/types",
VENUES_ENDPOINT: "services/venues"
};
//这是我的布局文件。
import constants from "../constants.js"
axios.get("http://localhost:8080/" + constants.SERVICES_TYPE_ENDPOINT)
.then(response => (this.services = response.data))
.catch(error => (console.log(error)));
我希望 constants.js 的字符串连接在 URL 的末尾,但我返回未定义。
最佳答案
您需要在 constants.js 文件中导出您的对象。
const endpoint_constants = {
COMPANIES_ENDPOINT: "users/companies",
SERVICES_ENDPOINT: "services",
SERVICES_TYPE_ENDPOINT: "services/types",
VENUES_ENDPOINT: "services/venues"
};
export default endpoint_constants
然后像这样导入它,
import endpoint_constants from './constants.js';
axios.get("http://localhost:8080/" + endpoint_constants.SERVICES_TYPE_ENDPOINT)
.then(response => (this.services = response.data))
.catch(error => (console.log(error)));
关于javascript - 有没有办法在 javascript 中创建一个字符串资源文件,我可以在我的 Vue 代码中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56164117/