0

I have the following simple structure (backend is using Python Flask):

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
</head>
<body>
    <script type="module" src="static/js/AClass.js"></script>
    <script type="text/javascript">
        var logging_url = "http://127.0.0.1:5000";
        var a_class = new AClass("some data");
        a_class.initialize();
    </script>
</body>
</html>

And in my static/js folder I have the following files:

static/js/AClass.js:

import DataManager from './DataManager.js';

class AClass {

    constructor(constructor_param) {
        this.datamanager = undefined;
    }

    initialize() {
        console.log("Initializing...");
        this.datamanager = new DataManager();
    }

}

DataManager.js currently is an empty class in static/js/

When I run this stuff in my browser I the get following error:

Uncaught ReferenceError: AClass is not defined

However, I do not know why?

enter image description here

toom
  • 12,864
  • 27
  • 89
  • 128
  • See https://stackoverflow.com/a/59539045 - the top level of an ES6 module is not global, so your non-module script can't see the variable at the top level of the module. Either assign `AClass` to the window, or put the functionality that calls `AClass` into a module which imports `AClass` – CertainPerformance Feb 29 '20 at 12:26
  • Well, I am not a Javascript expert. And this answer is too theoretical. I need some example of how to do that. Thus this comment might direct me in the right direction but it is not a solution to my question. – toom Feb 29 '20 at 12:28
  • `Either assign AClass to the window` isn't exactly theoretical. To assign a property to an object, you can use `=`, which is the assignment operator - eg `window.prop = 'someVal'` assigns `someVal` to the `prop` property of`window`. You can do the same thing with your `AClass`. – CertainPerformance Feb 29 '20 at 12:31
  • Did you try using `=` to assign to the `AClass` property of the window? That really is the solution (well, one of them), please try it – CertainPerformance Feb 29 '20 at 12:40

0 Answers0