简体   繁体   中英

javascript modules export updated variable

I'm new to js modules and I don't fully understand how they works, my problem is;

I have this fixed var that I need in more modules so at the moment my code is:

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
}

and in my app, it's another module, I do:

import functions from './functions'

const initialize = () => {
  // update the settings
  functions.init()
// initialize the map
.then(() => mapInit(functions.getFixedSettings()))
}

but when I ask for functions.getFixedSettings() in another module I need to call again functions.init() before to have the updated var

What is the correct way to export that updated fixed var and be accessible to all my modules?

如果您希望fixed对所有模块都可访问,则可以只输出fixedexport let fixed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM