【发布时间】:2018-12-21 15:07:01
【问题描述】:
我是 js 模块的新手,我不完全理解它们是如何工作的,我的问题是;
我在更多模块中需要这个 fixed var,所以目前我的代码是:
let fixed
const getFixed = () => {
return $.getJSON('data/fixed.json')
.done((data) => {
fixed = data.settings[0]
})
.fail((jqxhr, textStatus, error) => {
const err = `${textStatus} , ${error}`
alert(`fixed.json for getSettings Request Failed: ${err}`)
})
}
const init = () => {
return getFixed()
}
const getFixedSettings = () => {
return fixed
}
export default {
init,
getFixedSettings
}
在我的应用程序中,它是另一个模块,我这样做:
import functions from './functions'
const initialize = () => {
// update the settings
functions.init()
// initialize the map
.then(() => mapInit(functions.getFixedSettings()))
}
但是当我在另一个模块中请求 functions.getFixedSettings() 时,我需要再次调用 functions.init() 才能获得更新的 var
导出更新的fixed var 并可供我的所有模块访问的正确方法是什么?
【问题讨论】:
标签: javascript webpack es6-modules