YogenGhodke
3/25/2020 - 9:48 AM

Modules

Implementing Modules to pass data between multiple files.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Modules</title>
  </head>
  <body>
    <pre style = "font-size:3em" id = "msg"> asdasd </pre> 
    <script src="bundle.js"></script>
  </body>
</html>
import {data} from './data.js';                 // Can only import parsed JSON data.
const message = data[0].abc;

document.getElementById('msg').textContent = message;
// Dataset is not an Array. It is a Stringified Array JSON.stringify(array). 
const dataset = `[
  {
    "abc": 123,
    "def": 456
  },
  {
    "abc": 999,
    "def": 888
  },
  {
    "abc": 777,
    "def": 333
  }
]`;

export const data = JSON.parse(dataset);        // String converted back to Array while passing.