I have a class with a static method that I am trying to call from my html page. When I try to call the method using Class.Method() syntax I get an eror: "<Class name > is not defined". When I call that same static method from instances of other javascript classes the static methods work fine.
I managed to kludge a workaround together but it does not look right:
window.dlg = Dialogs; // static methods only // do I have to do this?
This question appears to validate my belief that I should be able to call my method using Class.Method() syntax.
This question appears to address the issue but does not explain how a static method might be called from html.
What am I doing wrong?
// main.js
import { Site } from './site.js';
import { Dialogs } from './dialogs.js';
(async function ($) {
"use strict";
window.site = new Site(); // instance methods
// hack:
window.dlg = Dialogs; // static methods only // do I have to do this?
await window.site.Init();
})(jQuery);
// dialogs.js
class Dialogs {
static ShowLoginDialog() {
$('#login-dialog').modal('show');
}
}
export { Dialogs };
// html
// works
<a href="#" onclick="window.dlg.ShowLoginDialog()"><i class="icofont-login"/> Log in</a>
// does not work but I think it should. Error msg: Dialogs is not defined.
<a href="#" onclick="Dialogs.ShowLoginDialog()"><i class="icofont-login"/> Log in</a>