【发布时间】:2019-09-11 03:34:57
【问题描述】:
在反应中,我在 js 文件中声明了一个 const 变量,将其附加到 index.html
在我的反应文件 App.js 中,如果我使用变量:
- run react-scripts start/build got 'myVariable' is not defined no-undef
- 运行 webpack,html 工作正常
my example in codeSandbox(注意:codeSandbox 示例也可以正常工作)
公共文件夹中的index.html(添加了config.js)
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="config.js"></script>
</body>
webpack生成的dest文件夹中的index.html
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="config.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
config.js(声明变量服务器)
const server = 'my server';
App.js(直接在 react 脚本中使用变量)
function App() {
return (
<div className="App">
this is server: {server}
</div>
);
}
我想知道:
- 使用
react-scripts start/build时是否可以通过编译错误? - 使用webpack,如何检查这种未定义变量的问题?
其他问题:
- 如何使用 webpack/webpack-dev-server 检查 no-unused-vars?
例如使用react-scripts start可以获得警告:'myVariable' is assigned a value but never used
感谢您的帮助!
【问题讨论】:
标签: reactjs webpack webpack-dev-server react-scripts