skip to Main Content

I have static files in django call it A.js and B.js

in A.js I have for example for constant like

const variable = "Hello, World"
export {variable}

in B.js I have:

import {variable} from './A.js'

Kindly note that both files are in the static files directory of django project.

My question is how am I going to import successfully the variable so that it checks into variable and use them in the template without getting exceptions

Uncaught SyntaxError: Unexpected token ‘export’ and the whole JS does not work

I also needed to declare them as modules inside the template

 <script src='{% static "js/A.js" %}' type="module" ></script>
 <script src='{% static "js/B.js" %}' type="module" ></script>

either ways not working. I have previously used export default approach , still not working

2

Answers


  1. Chosen as BEST ANSWER

    Okay guys I found that django does not require webpacks import or export statement in order to access javascript variables, Remember the `**

    python manage.py collectstatic

    **` , it does the job of managing or you can also say bundling your django static files into a single file

    In ruby on rails , there is an application.js or application.css, which is a root of all the other javascripts or css file within the assets folder of the rails framework

    In a similar way, django does not specify a single file but its designed to automatically make all the files in a static folder visible to each other

    thus without any import statement or export statement I can easily call the variable in B.js which is defined in A.js


  2. Try doing

    export default variable
    

    instead of

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