简体   繁体   English

无法从 html 调用 static 方法

[英]Cannot call static method from html

I have a class with a static method that I am trying to call from my html page.我有一个 class 和我试图从我的 html 页面调用的 static 方法。 When I try to call the method using Class.Method() syntax I get an eror: "<Class name > is not defined".当我尝试使用 Class.Method() 语法调用方法时出现错误:“<Class name > is not defined”。 When I call that same static method from instances of other javascript classes the static methods work fine.当我从其他 javascript 类的实例调用相同的 static 方法时,static 方法工作正常。

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. 这个问题似乎证实了我的信念,即我应该能够使用 Class.Method() 语法调用我的方法。

This question appears to address the issue but does not explain how a static method might be called from html. 这个问题似乎解决了这个问题,但没有解释如何从 html 调用 static 方法。

What am I doing wrong?我究竟做错了什么?

// main.js // 主.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 // 对话框.js

class Dialogs {
    static ShowLoginDialog() {
        $('#login-dialog').modal('show');
    }
}
export { Dialogs };

// html // html

// works            
<a href="#" onclick="window.dlg.ShowLoginDialog()"><i class="icofont-login"/>&nbsp;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"/>&nbsp;Log in</a>

Avoid inline handlers , they have way too many problems to be worth using in a modern codebase.避免内联处理程序,它们有太多问题,不值得在现代代码库中使用。 Since you're using modules, the Dialogs won't be available globally unless you explicitly assign it to the global object which, as you've noticed, is an ugly hack (and inline handlers can only reference global variables).由于您使用的是模块,因此除非您明确地将其分配给全局 object ,否则Dialogs将无法全局使用,正如您所注意到的,这是一个丑陋的 hack(内联处理程序只能引用全局变量)。 You should attach the event listener properly using Javascript instead: select the element inside your script, and call addEventListener on it:您应该使用 Javascript 正确附加事件侦听器:select 脚本中的元素,并对其调用addEventListener

// main.js

import { Site } from './site.js';
import { Dialogs } from './dialogs.js';

(async function ($) {
    "use strict";
    window.site = new Site();   // instance methods

    document.querySelector('a.login-anchor').addEventListener('click', () => {
        Dialogs.ShowLoginDialog()
    });
    await window.site.Init();
})(jQuery);

where a.login-anchor can be replaced with whatever selector you want to target the <a> with the listener:其中a.login-anchor可以替换为您想要使用监听器定位<a>的任何选择器:

<a href="#" class="login-anchor"><i class="icofont-login"/>&nbsp;Log in</a>

You also don't need to put site onto the global object either - put into a variable scoped just to the module instead:您也不需要将site放到全局 object 中 - 而是放入一个仅限于模块的变量中:

const site = new Site();
await site.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM