[英]Unexpected token '<' Error (npx create-react-app)
I've setup a new react app using npx create-react-app my-app
.我已经使用 npx
create-react-app my-app
设置了一个新的反应应用程序。
In the index.html file I want to import a javascript file in the head like so在 index.html 文件中,我想像这样在头部导入一个 javascript 文件
<script src="/src/.../file.js"></script>
However, I keep getting Unexpected token '<' error.但是,我不断收到 Unexpected token '<' 错误。 I can't do an import as it creates loads of errors and I want to be able to use some functionality in these scripts across the site.
我无法进行导入,因为它会产生大量错误,并且我希望能够在整个站点的这些脚本中使用某些功能。
Any ideas how to reference this file?任何想法如何引用这个文件? Previously in other projects we use webpack but this config doesn't seem available in this project.
以前在其他项目中我们使用 webpack 但此配置在此项目中似乎不可用。
If your js files are located in the /public/ folder then you can import it like this:如果您的 js 文件位于 /public/ 文件夹中,那么您可以像这样导入它:
<script src="%PUBLIC_URL%/.../file.js"></script>
If it is located in /src/ folder, you can simply import it to your index.js
file, and it will be available across your React application.如果它位于 /src/ 文件夹中,您只需将其导入您的
index.js
文件,它将在您的 React 应用程序中可用。 Remember that order of imports matters here.请记住,进口顺序在这里很重要。
import './.../file.js';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.