【问题标题】:React JS Uncaught ReferenceError : require is not definedReact JS Uncaught ReferenceError:未定义要求
【发布时间】:2018-01-24 02:29:59
【问题描述】:

我目前正在尝试通过 CDN 让 React 工作。

当我手动运行 index.html 文件时,它可以工作。但是当我将文件放到像 XAMPP 这样的网络服务器上时,我会遇到

未捕获的引用错误:未定义要求

index.html:

<html>
    <head>
        <title>TestApp</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
        <script src="./node_modules/moment/moment.js"></script>
    </head>
    <body>
        <div id="app"></div>
        <script type="text/babel" src="./react.js"></script>
    </body>
</html>

react.js:

const Aest = ({test}) =>  {
    return (
        <div>WATss {test}</div>
    )
}
class Test extends React.Component{
    constructor(){
        super()
        this.state={
            test: 0
        }
    }
    inc(){
        this.setState({
            test: ++this.state.test
        })
    }
    render(){
        return(
            <React.Fragment>
                <div>test: {this.state.test}</div>
                <button onClick={()=>this.inc()}>Add</button>
                <Aest test={this.state.test}/>
            </React.Fragment>
        )
    }
}
ReactDOM.render(<Test />,document.getElementById('app'));

【问题讨论】:

  • 你在使用 webpack 和 babel 来打包/转换你的 js/jsx 吗?
  • 我正在使用 babel cdn

标签: javascript reactjs babeljs


【解决方案1】:

在某些网络服务器中默认不理解require,以便首先识别它。使用require js。 npm 安装它。并导入它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2016-07-29
    相关资源
    最近更新 更多