vue 的 object 自己用自己的值?

const configCommon = {
  defaultLang: 'ch',
  productionHost: 'abc.com',
  apiHost: location.host.indexOf('abc.com') > -1 ? 'https://api.abc.com' : 'http://127.0.0.1:8200',
  deviceWidth: 500,
  ...
}

export default configCommon

有個疑問
假設我的 abc.com 重複了至少三次
可以在object中自己用自己?
我的想像畫面是...

const configCommon = {
  productionHost: 'abc.com',
  apiHost: location.host.indexOf(configCommon.productionHost) > -1 ? `https://api.${configCommon.productionHost}` : 'http://127.0.0.1:8200',
  ...
}

當然,獲得了錯誤...

Cannot access 'configCommon' before initialization
阅读 1.6k
2 个回答

我会这么处理

const baseUrl = 'abc.com'
const configCommon = {
  defaultLang: 'ch',
  productionHost: baseUrl,
  apiHost: location.host.indexOf(baseUrl) > -1 ? `https://api.${baseUrl}` : 'http://127.0.0.1:8200',
  deviceWidth: 500,
  ...
}

export default configCommon

没有性能要求的话,做成getter字段用this访问。

否则就先留空,下一条语句再赋值这个字段。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题