Html不读取我的js功能 [英] Html Doesnt read my js functions

查看:349
本文介绍了Html不读取我的js功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用react来创建幻灯片,问题是我的html没有找到更改幻灯片的函数。
就像这样:

Im using react to create a slideshow, the problem is that my html doesnt find the function to change the slide. It goes like this:

<!DOCTYPE html>
<html>
<body>
  <header Access-Control-Allow-Origin: *>
  </header>
  <main id="peli">
    <div id="root">
    </div>
    <script src="dist/app.bundle.js">plusSlides(2)</script>
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
  </main>
</body>
</html>

在我的js文件中,我有一个ReactDOM.render来呈现我的html和一个函数来更改幻灯片,加滑道,但由于某种原因程序不能找到函数plusSlides。我使用webpack编译我的文件。

In my js file I have a ReactDOM.render to render my html and a function to change the slides, plusSlides, but for some reason the program cant find the function plusSlides. I use webpack to compile my file.

推荐答案

Webpack通常不会将东西暴露给窗口。您需要从javascript初始化。

但是您可以告诉webpack https://webpack.js.org/guides/author-libraries/#expose-the-libraryrel =nofollow noreferrer>导出库,它可以像你想要的那样工作。

Webpack normally doesn't expose things to window. You are expected to initialise from javascript.
But you can tell webpack to export a library which will work like you want.

您只需将其添加到输出配置中即可。

All you have to do is add this to the output config.

output: {
    filename: 'app.js',
    library: 'webpackLib',
    libraryTarget: 'window',
},

您对目标有不同的选项,请查阅文档获取更多信息。

You have different options for the target, look in to documentation for more info.

这篇关于Html不读取我的js功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆