简体   繁体   English

我可以在JavaScript中全局使用导入的函数和类吗?

[英]Can I use imported functions and classes globally in JavaScript?

I want to switch from all classes in the html header file to the module approach. 我想从html标头文件中的所有类切换到模块方法。

I want to use some methods and function on buttons and other elements. 我想在按钮和其他元素上使用一些方法和功能。

In my index.html in the head section: 在我的index.html的头部部分:

<script src="js/main.js" type="module" defer></script>

Inside the main.js: 在main.js中:

import {writeAlert} from './test.js';

Inside the test.js: 在test.js中:

export function writeAlert () {
    alert('I am an alert');
}

Inside the body element of the index.html 在index.html的body元素内部

<button onlick="writeAlert()">Alert</button>

I am getting the error message that the function (or in other cases class) is not defined. 我收到错误消息,即未定义函数(或在其他情况下为类)。 So what is the way to implement a global use of imported functions? 那么,实现导入功能的全局使用的方法是什么? Is the client actually able to see my whole source code with that approach? 客户实际上可以通过这种方法查看我的整个源代码吗?

Identifiers imported in a module are not visible on the outside. 在模块中导入的标识符在外部不可见。

Two solutions, in order of personal preference: 两种解决方案,按个人喜好顺序排列:

  1. Move the callback installation from HTML to JS: 将回调安装从HTML移到JS:
    • Change the button definition to: <button id="alertButton">Alert</button> 将按钮定义更改为: <button id="alertButton">Alert</button>
    • Add the following code to main.js , or another module that imports writeAlert : 将以下代码添加到main.js或另一个导入writeAlert模块中:
     document.addEventListener("DOMContentLoaded", () => { document.getElementById("alertButton").onclick = writeAlert; }); 
  2. "Export" writeAlert onto window in one of the modules: “导出” writeAlert到模块之一中的window上:
     import { writeAlert } from "./test.js"; window.writeAlert = writeAlert; 
    In this way, writeAlert is accessible anywhere in your code, HTML or JS, without any imports. 这样,无需任何导入即可在代码,HTML或JS中的任何位置访问writeAlert But this rather defeats the purpose of using imports in the first place and isn't as elegant. 但这恰恰违背了使用导入的目的,并且不那么优雅。

This is a patchy answer. 这是一个不完整的答案。 But, the same idea can be extended further. 但是,相同的想法可以进一步扩展。

In main.js: 在main.js中:

import {writeAlert} from './test.js';
window.writeAlert = writeAlert;

After that, “writeAlert” is accessible from HTML file. 之后,可以从HTML文件访问“ writeAlert”。

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

相关问题 为什么我不能使用require()导入的javascript文件中的函数? - Why can't I use functions from javascript-files that are imported by require()? 我什么时候应该在 Javascript 中将函数用作类? - When should I use functions as classes in Javascript? 如何在Drupal 7中全局使用javascript函数 - How can I use a javascript function globally in Drupal 7 当您只可以在Javascript中全局调用函数时,为什么还要使用回调? - Why use callbacks when you can just call functions globally in Javascript? 我可以使用带有javascript模块模式的类吗? - Can I use classes with the javascript module pattern? 如果在该代码中导入了一些模块,我可以使用普通的 javascript 代码吗 - can i use normal javascript code if in that code imported some module 我可以为像 javascript 这样的 PHP 默认类创建新函数吗? - Can I create new functions for PHP default classes like javascript? 我可以在 javascript 的任何地方使用异步函数吗? - Can i use async functions everywhere in javascript? 您可以直接在 Vue 组件的模板中使用导入的 JavaScript 库中的函数,例如 Change Case 吗? - Can you use functions from an imported JavaScript library such as Change Case directly in a Vue component's template? 导入外部javascript文件并在vue app中全局使用函数 - Import external javascript file and use functions globally in vue app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM