我在我的 React 代码中编写以下内容:
var React = require('react');
我遵循 tutorialspoint 中的 React 设置。我将所有东西安装在/Desktop/reactApp/中。
React 代码从 /GitProjects/DjangoProjects/MyProj/MyApp/static/react/dashboard.js
运行。我不断收到错误Uncaught ReferenceError
。我错过了什么吗?
注意:/GitProjects/DjangoProjects/MyProj/MyApp/templates/dashboard.html
处的 HTML 文件正在调用我的 dashboard.js 代码。
最佳答案
听起来您正尝试在 HTML 中包含文件 dashboard.js
。
如果这个文件包含像 require('react');
这样的代码,那么这意味着您需要首先使用一个构建工具来编译它,该工具实际上会找到 react
和将其与您自己的代码捆绑在一起。
对于您链接的教程,使用的构建工具是webpack
。您的 HTML 应包含由 webpack 生成的文件(index.js
,或 webpack 配置的 output
部分中的任何内容),而不是 dashboard.js
单独。
webpack.config.js
var config = {
entry: './main.js', # the main code of your app that require()s other files
output: {
path:'./',
filename: 'index.js', # the file you should be including in HTML
},
关于javascript - Uncaught ReferenceError : require is not defined React. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38815405/