简体   繁体   English

为什么我的外部 JS 文件不会加载到 ReactJS 中?

[英]How come my external JS file won't load in ReactJS?

I can't for the life of me figure out why this external JS file isn't loading.我一生都无法弄清楚为什么这个外部 JS 文件没有加载。 I've tried every help question out there and it still won't work so maybe someone else can see what I can't.我已经尝试了所有帮助问题,但它仍然无法正常工作,所以也许其他人可以看到我看不到的东西。

So I'm working on a ReactJS project and I'm trying to link an external JS file.所以我正在研究一个 ReactJS 项目,我正在尝试链接一个外部 JS 文件。

Here is the file structure:这是文件结构: 在此处输入图像描述

The external javascript file is in public/js/script.js .外部 javascript 文件位于public/js/script.js中。

I've tried every method I've found to get it to work.我已经尝试了所有我发现的让它工作的方法。

I've linked it in index.html as follows:我已将其链接到index.html中,如下所示:

<script src="./js/script.js" type="text/jsx"></script>

I've added the following to my main App component:我已将以下内容添加到我的主要 App 组件中:

    componentDidMount() {
        const script = document.createElement('script');
        let url = '../public/js/script.js'
        script.src = url;   //(This is external js url)
        script.async = true;
        document.body.appendChild(script);
      }

I've added this into the functional component where I really need the external javascript to work:我已将其添加到我真正需要外部 javascript 工作的功能组件中:

      useEffect(() => {
        const script = document.createElement('script');
        script.src = '../public/js/script.js';   //(This is external js url)
        script.async = true;
        document.body.appendChild(script);
      }, [])

And yet not matter any of these attempts nothing seems to make the file work.然而,这些尝试中的任何一个似乎都无法使文件正常工作。

I am running the project on localhost, so I'm not sure if there's an issue there.我在本地主机上运行该项目,所以我不确定那里是否存在问题。 I'd just like to figure out how to make external javascript files work.我只是想弄清楚如何使外部 javascript 文件工作。 I've tried using Helmet.我试过用头盔。

I don't know what I'm doing wrong.我不知道我做错了什么。 Anyways, appreciate any help I can get trying to figure this out.无论如何,感谢我试图解决这个问题的任何帮助。

Edit: I did adjust the following: src="./js/script.js to src="js/script.js in the <script> tag and it also has had no effect.编辑:我确实调整了以下内容: src="./js/script.jssrc="js/script.js<script>标记中,它也没有效果。 Still looking for a solution.仍在寻找解决方案。

Original Code:原始代码:

<script src="./js/script.js" type="text/jsx"></script>

Changed code: (changed to relative directory)更改代码:(更改为相对目录)

<script src="js/script.js" type="text/jsx"></script>

Put /js/script.js inside public folder, then add this line beetwen head tag./js/script.js放在 public 文件夹中,然后添加这一行 beetwen head标签。 (no dot prefix) (无点前缀)

If you put under body tag, this line will be overwritten when React start to render elements.如果你放在body标签下,当 React 开始渲染元素时,这一行将被覆盖。

<script src="/js/script.js" type="text/jsx"></script>

Or, another way;或者,另一种方式; try to change this line:尝试更改此行:

let url = '../public/js/script.js'让 url = '../public/js/script.js'

to:至:

let url = `${process.env.PUBLIC_URL}/js/script.js`让 url = `${process.env.PUBLIC_URL}/js/script.js`

This solution worked for me这个解决方案对我有用

  1. I used useEffect hook inside a function component (before return).在 function 组件中使用了 useEffect 钩子(在返回之前)。 This can be used inside App.js also before return.这也可以在返回之前在 App.js 中使用。 Note: if you want to do this inside a class which extends React.component then you need to use componentDidMount() (before render), this is required as hooks work inside a function component or custom hooks only.注意:如果您想在扩展React.component的 class 中执行此操作,则需要使用componentDidMount() (在渲染之前),这是必需的,因为挂钩在 function 组件或仅自定义挂钩中工作。
  2. In the header add a script tagheader 添加脚本标签
  1. Small Snippet of the code:代码小片段:

     import React, from "react"; React.useEffect(() => { const LoadExternalScript = () => { const externalScript = document.createElement("script"); externalScript.onerror = loadError; externalScript.id = "external"; externalScript.async = true; externalScript.type = "text/javascript"; externalScript.setAttribute("crossorigin", "anonymous"); document.body.appendChild(externalScript); externalScript.src = './script.js'; }; LoadExternalScript(); return () => { // document.body.removeChild(externalScript); }; }, []);
  2. You will find this link useful if you get a syntax error:如果您遇到语法错误,您会发现此链接很有用:

"Uncaught SyntaxError: Unexpected token < in React" Syntax error solution “Uncaught SyntaxError: Unexpected token < in React” 语法错误解决方案

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM