How to bring an array that is in a separate file to the application?

Asked

Viewed 56 times

3

I have a JS (data.js) file that contains an array of the information below:

 module.exports = [
        {
          "photo": "https://a0.muscache.com/im/pictures/e6c4b347-49c7-4840-8c00-df36a2a273da.jpg?aki_policy=x_large",
          "property_type": "Apartamento",
          "name": "Apartment in Son Parc, wonderful views",
          "price": 433
        },
        {
          "photo": "https://a0.muscache.com/im/pictures/4a5326cb-95e4-4220-a4d8-c91f50cf784c.jpg?aki_policy=xx_large",
          "property_type": "Apartamento",
          "name": "APARTAMENTO IDEAL PAREJAS EN SON PARC",
          "price": 368
        },
      ]

and I need to use this information in HTML. How do I make Javascript recognize this file? I am trying this command, but it gives error.

const dados = require('./dados.js');
console.log(dados);

I’m doing the console.log to test if I brought the information, then I continue the work.

  • But you are trying to import this data into another javascript or html file?

  • I am placing the import in Java.

1 answer

-1


Editing the reply as per comment below:

<html>
  <head>    
    <script type="module" src="index.js"></script>
  </head>
  <body>    
  </body>
</html>

index js.

import dados from '/dados.js';

console.log(dados);

dice js.

export default [
  {
    "photo": "https://a0.muscache.com/im/pictures/e6c4b347-49c7-4840-8c00-df36a2a273da.jpg?aki_policy=x_large",
    "property_type": "Apartamento",
    "name": "Apartment in Son Parc, wonderful views",
    "price": 433
  },
  {
    "photo": "https://a0.muscache.com/im/pictures/4a5326cb-95e4-4220-a4d8-c91f50cf784c.jpg?aki_policy=xx_large",
    "property_type": "Apartamento",
    "name": "APARTAMENTO IDEAL PAREJAS EN SON PARC",
    "price": 368
  },
]
  • 1

    If the browser does not support Ecmascript6 you can make the Imports with the module.Xports normally!

  • I changed the script to: js&#xA;import dados from '/dados.js';&#xA;&#xA;console.log(dados);&#xA; , and the following message appeared: import data from '/data.js'; Syntaxerror: Cannot use import statement Outside a module at Module. _Compile (Internal/modules/cjs/Loader.js:891:18)

  • Please specify which server, framework you are using.

  • i am doing an activity that suggested using pure Javascript. I don’t know if I didn’t get it right, but I was thinking that I could do it without installing anything, just setting it up right in the scrips file... but if there’s no way I know a little of the express

  • You are including the script via html as I put in the reply?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.