Html Doesnt read my js functions

2019-09-21 05:08发布

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>

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.

1条回答
女痞
2楼-- · 2019-09-21 05:17

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.

查看更多
登录 后发表回答