简体   繁体   English

如何在文档就绪功能中使用模块文件

[英]How can I use module file in the document ready funciton

There is a config module file contained some enviorment paramters, I want to call it in the document ready function.有一个配置模块文件包含一些环境参数,我想在文档准备函数中调用它。 Because the document ready function is firstly called, it will throw undefined error.The config file has been already used in other module files.因为第一次调用文档准备函数,会抛出未定义的错误。配置文件已经在其他模块文件中使用。 Could someone make any suggestions here?有人可以在这里提出任何建议吗? Thanks谢谢

kconfig.js

    const kconfig = (function () {
    function kconfig() {

    }

    kconfig.webApiUrl = "http://localhost/webapi/"

    return kconfig;
})()

export default kconfig;

index.html page index.html 页面

    <script type="module" src="../../config/kconfig.js"></script>
    <script type="text/javascript">
        $(function () {
            console.log('document ready...')
            console.log(kconfig.webApiUrl)
            //$.ajaxGet(kconfig.webApiUrl + 'api/hello/query/123')
        });
    </script>

error detail:错误详情:

Uncaught ReferenceError: kconfig is not defined

Why are you requiring kconfig and then also importing it?为什么需要 kconfig 然后还要导入它? import is just the es6 implementation of require. import 只是 require 的 es6 实现。 You should just be doing import kconfig from '../../config/kconfig';你应该只是从'../../config/kconfig'导入kconfig; and not the line after it.而不是它之后的行。 Also, you're not importing kconfig in the script that it's used in, that may cause a problem although I dont think it would because the first script tag should be executed first and then be accessible globally.此外,您没有在使用它的脚本中导入 kconfig,这可能会导致问题,尽管我认为这不会因为第一个脚本标签应该首先执行,然后可以全局访问。 Hope this points you in a helpful direction.希望这能为您指明一个有用的方向。

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

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