我正在使用单个文件组件开发几个Vue应用程序.我发现我的很多组件需要公共配置信息,例如包含传递方法的对象,我可能会这样定义:
const DeliveryMethods = { DELIVERY: "Delivery",CARRIER: "Carrier",COLLATION: "Collation",CASH_AND_CARRY: "Cash and carry" }
什么是使我的组件可用的最简单最简单的方法?目前,我已经使用’config.js’文件完成了它,如下所示:
export default { DeliveryMethods: { DELIVERY: "Delivery",CASH_AND_CARRY: "Cash and carry" } }
在我需要它的组件中,我有来自’src / config.js’的导入配置,并且我想使用其中一个,我将参考例如config.DeliveryMethods.CASH_AND_CARRY.这在我看来相当冗长和重复,但我更喜欢能够使用DeliveryMethods.CASH_AND_CARRY而不是config.DeliveryMethods.CASH_AND_CARRY.其他人用什么来完成这个?
解决方法
递送方法/ index.js
const DELIVERY = "Delivery" const CARRIER = "Carrier" const COLLATION = "Collation" const CASH_AND_CARRY = "Cash and carry" export default { DELIVERY: DELIVERY,CARRIER: CARRIER,COLLATION: COLLATION,CASH_AND_CARRY: CASH_AND_CARRY }
import DeliveryMethods from './path/to/delivery-methods' console.log(DeliveryMethods.CARRIER)
要么:
config.js
export default Object.freeze({ DELIVERY: "Delivery",CASH_AND_CARRY: "Cash and carry" })
用法:
import DeliveryMethods from './path/to/delivery-methods' console.log(DeliveryMethods.CARRIER)