我是js模块的新手,我不完全了解它们的工作原理,我的问题是
我有更多的模块需要这个fixed
变量,所以目前我的代码是:
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并可供我所有模块访问的正确方法是什么?
答案 0 :(得分:0)
如果您希望fixed
下注所有模块都可以访问,则可以只导出fixed
:export let fixed
。