我想摆脱我与Axios一起使用的所有硬编码端点。因此,我想将它们替换为资源文件。
我已经创建了一个常数。当我参考此文件中的值时,我会看到来自常数的字符串值不确定。
//这是常量。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)));
我希望在URL的末端连接常数。
您需要将对象导出在startants.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)));