skip to Main Content

I wish to reference the js data file in the framework

enter image description here

I write like this

import Configs from "../data/configs";
console.log(Configs);

in chrome no output

use export same not working

const Configs = [
  {
    "id": 0,
    "cover": "1.webp"
  }
]
export default Configs

2

Answers


  1. Try to put the console.log in <script> like this

    ---
    import Layout from '../layouts/Layout.astro';
    
    ---
    
    <script>
    import Configs from '../data/config';
    
    console.log(Configs);
    </script>
    
    <Layout title="Welcome to Astro.">
    

    See more details for the client side script:

    Login or Signup to reply.
  2. Not entirely clear what framework you are using client side but some documentation for you to look through for this is here if working with React/Vue etc. Otherwise to run standard JS with all code contained within the .astro file then passing your console log within a script tag will get the outcome you want.

    To summarise what is happening, you’ve written the import and console log within the — brackets. This is marking code to run on server side for this page. This will result in the log only appearing within a server console, with the client being completely unaware of any of this happening. Adding the script tags and the log within there is you telling the client to print that log.

    Worth noting though if you are only wanting to examine the code in the console then as I said before it will be appearing in the terminal where you ran your command to host your dev code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search