【问题标题】:Adding js script to the React project在 React 项目中添加 js 脚本
【发布时间】:2020-11-23 01:37:05
【问题描述】:

如何向 react 项目添加一个简单、经典的 js 脚本? 我试图通过添加来添加它

<script src="../src/alert.js"></script>

index.htmlhead 中,但它似乎不起作用!

【问题讨论】:

  • 您是否尝试将其添加到index.js -> import './alert.js';
  • 谢谢,这有帮助,但为什么我不能从主 index.html 文件中添加它?

标签: javascript reactjs


【解决方案1】:

Here the Dan Abramov's answer say

在底层,Create React App 使用 Webpack 和 html-webpack-plugin.

index.html 用作模板,index.js 是您应用的入口点

  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>

因此,如果您想加载其他脚本,您必须将它们导入到 index.js 中。

【讨论】:

  • 好的,谢谢。我明白。不要生气,但我会问 - 当我使用经典 js &lt;script&gt; 标签添加脚本时,为什么它不起作用?
  • 你能这么好,回答我最后一个问题吗?
猜你喜欢
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
  • 2020-07-06
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多