从HTML脚本中使用浏览器中的ES6模块导出
我正在将旧的JavaScript代码转换为ES6模式,并且正在发现许多问题,因为我的旧技术是动态生成许多HTML标签,其中包括这样的标签中的硬编码事件处理程序:
const html = '<input type="text" onclick="doThisOnClick();"/>';
getElementFromID('hostId').innerHTML = html;
这些处理程序在ES6模块中并删除了因为在加载时间没有任何脚本中都没有直接使用它们,也不会通过其他模块代码调用它们。 解决此问题的唯一方法是重新设计我的代码以在生成HTML标签后使用动态事件处理程序注入。必须通过settimeout()调用来完成此操作。 我一直在尝试发现其他方法,使ES6模块可访问潜在生成的HTML代码。 这是我遇到的一个特定问题:
<script type="module">
import { deepFunctionUsedByPotentialEventHandler } from './bar.js';
</script>
该网站由Nodejs托管:当地Dev-Server。 我从Chrome中获得了此错误:
htmlFile.html:17 Uncaught SyntaxError:
The requested module './bar.js' does not provide an export named
'deepFunctionUsedByPotentialEventHandler ' (at htmlFile.html:17:10)
但是该功能在bar.js中清楚地导出,并在bar.js中定义:
bar.js...
function deepFunctionUsedByPotentialEventHandler() {
...
}
export { deepFunctionUsedByPotentialEventHandler, foo, bar };
我怀疑BACAUSE在加载时间没有从模块中剥离的deepfuncunctusBypotentialeventhandhandler的deepfuncunctionBypotentialeventHandler,以使其更快地加载。 。 这不是使用WebPack或任何其他建筑类型工具。
我宁愿通过不使用窗口来保持全局范围清洁。DeepFunctionBypotentialeventhandler。
在我看来,ES6规格应该有一种方法来强制保留代码,例如:
export { foo keep, bar, bob }
我尝试了许多想法,但是到目前为止,我还没有找到一种快速移动的方法来转换此代码。
I am converting old javascript code to es6 modeules and am finding many issues because my old technique was to dynamically generate a lot of HTML tags which included hard-coded event handlers in the tags like this:
const html = '<input type="text" onclick="doThisOnClick();"/>';
getElementFromID('hostId').innerHTML = html;
Those handlers are in es6 modules and get cut out because there is no direct use of them in any scripts at load time nor are they called by other module code.
The only way to fix this I have found is to redesign my code to use dynamic event handler injection after generating the HTML tags. This must be done asynchronouosly via a setTimeout() call.
I have been trying to discover other ways to make es6 modules accessible to potentially generated HTML code.
Here is a specific issue I am having:
<script type="module">
import { deepFunctionUsedByPotentialEventHandler } from './bar.js';
</script>
The website is being hosted by nodejs:dev-server locally.
I get this error from chrome:
htmlFile.html:17 Uncaught SyntaxError:
The requested module './bar.js' does not provide an export named
'deepFunctionUsedByPotentialEventHandler ' (at htmlFile.html:17:10)
But the function is clearly exported in bar.js and is defined in bar.js:
bar.js...
function deepFunctionUsedByPotentialEventHandler() {
...
}
export { deepFunctionUsedByPotentialEventHandler, foo, bar };
I suspect that bacause there is no path to deepFunctionUsedByPotentialEventHandler at load time that the code has been stripped from the module for faster loading.
This is NOT using webpack or any other building type tool.
I would prefer to keep the global scope clean by not using window.deepFunctionUsedByPotentialEventHandler.
Seems to me that the ES6 spec should have a way to force keeping code available like:
export { foo keep, bar, bob }
I have tried a host of ideas but so far I have not found a quick-easy way to convert this code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论