如何用外部文件替换json内容?

时间:2017-12-15 18:33:44

标签: javascript

我需要将大配置文件与不同的本地JSON文件分开。我想知道如何将不同的JSON文件作为配置文件的一部分?配置文件也是JSON对象。但它有一个非常大的属性,每个属性都有超过300行代码。我需要将它们作为本地JSON文件分开,以使配置文件看起来更简单。代码详情如下:

module.exports = function ()  {
  this.nodeServer = 
  this.tiledLayerBaseURL = 
  this.featureLayerBaseURL = 
  this.TIME_TO_IDLE = 10; // If user has no action within 1 minute, enter idle phase
  this.TIME_TO_LOGOUT = 3600; // Force user to logout after one hour idle phase
  this.Scenarios = [
    //Too many code for each scenario, over 200 lines of code
    //Need to separate them to local json file
    //I wonder how can I include these separated json file as part of code here
    {
      scenarioName: "Canada",
      scenarioID: 1,
      thumbnail_url: "/assets/img/scenario1_icon.png",
      isActive: true,
      scenarioExtent: [],
      scenarioHeight: "1111",
      baseMap: "satellite",
      indoorBaseMap: "streets-night-vector",
      ground: "world-elevation",
      timezone: "GMT-0600 (MDT)",
      init_camera_pos: {
        center: [-114.134, 51.084],
        heading: 0,
        tilt: 0,
        zoom: 16,
        zoomZvalue:2500
      },
      ...
      ...
      ...

1 个答案:

答案 0 :(得分:0)

您可以拥有包含各种方案的Scenarios.js文件。然后将其导入配置文件。

// Scenarios.js
module.exports = [
  {},
  {},
  {},
  // etc
]

// Config file
import scenarios from "./Scenarios.js" // or use require()
module.exports = function () {
  // etc
  this.Scenarios = scenarios;
}