React JS Sementic UI 一直报错说:“ReferenceError: jQuery未定义”

6

我非常新手React,对于Semantic UI有疑问。

我正在跟随教程,在App.js中使用Semantic UI的Header组件,方法是通过包含以下代码:

    import { Header } from 'semantic-ui-css'

然而,每当页面编译使用此代码时,它总是返回像这样的错误消息:
ReferenceError: jQuery is not defined

./node_modules/semantic-ui-css/semantic.js

node_modules/semantic-ui-css/semantic.js:497

  494 | });

  495 | 

  496 | 

> 497 | })( jQuery, window, document );

  498 | 

  499 | /*!

  500 |  * # Semantic UI 2.4.1 - Form Validation

在寻找解决方案时,我读了很多建议,其中之一是运行

npm install -s jquery

我这么做了,但它仍然返回相同的错误消息。

如果您能告诉我该怎么做,我将不胜感激。


以下是我认为与问题有关的文件部分:

App.js

import React, { Component } from 'react';
//import './App.css';
import { Header } from 'semantic-ui-css'
import TeacherForms from './components/TeacherForm';
import CourseList from './components/CourseList';
import { Col, Row, Container, Navbar, Button} from  'react-bootstrap'
import { connect } from 'react-redux';
import * as courseAction from './actions/courseAction';
import DynamicForm from './components/DynamicForm'

class App extends Component {
  constructor(props){
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);

    this.state = {
      name: ''
    }
  }
  
  handleChange(e){
    this.setState({
      name: e.target.value
    })
  }

  handleSubmit(e){

    e.preventDefault();
    let course = {
      name: this.state.name
    }
    this.setState({
      name: ''
    });
    if (course.name == ''){
      window.alert('Please enter a course name.')
    }else{
      this.props.addCourse(course);
    } 
  }

  listView(data, index){
    return (
      <div className="row">
        <div className="col-md-10">      
              <Button className = "text-left " variant = "light" block>
                {data.name}
              </Button>
        </div>
        <div className="col-md-2">
          <confirm>
            <button onClick={(e) => {if (window.confirm('Are you sure you want to delete this item?'))
              this.deleteCourse(e, index)
            }} className="btn btn-danger">
                Remove
            </button>
          </confirm>
        </div>
    </div> 
    )
  }

  deleteCourse(e, index){
    e.preventDefault();
    this.props.deleteCourse(index);
  }

  render(){
    let name;
    return ( 
      <div className="full-page">
        <Navbar bg="dark" variant="dark">
          <Navbar.Brand href="#home">Brandeis Course Scheduling</Navbar.Brand>
        </Navbar>
        <Row>
        <Col>
          <div className="course-list">
                <h1> Academic Requirements Form </h1>
                <hr />
                <div class = "overflow">
                <h3>Add Course</h3>
                <form onSubmit={this.handleSubmit}>
                    <input type="text" onChange={this.handleChange} className="form-control" value={this.state.name}/><br />
                    <input type="submit" className="btn btn-success" value="ADD"/>
                </form>
                <hr />
                { <ul className="list-group">
                  {this.props.courses.map((course, i) => this.listView(course, i))}
                </ul> }
                </div>
            </div>

        </Col>
        <Col>
          <TeacherForms></TeacherForms>
        </Col>
        </Row>
      </div>
    )
  }
}

const mapStateToProps = (state, ownProps) => {
  return{
    courses: state.courses
  }
};

const mapDispatchToProps = (dispatch) => {
  return {
    addCourse: course => dispatch(courseAction.addCourse(course)),
    deleteCourse: index => dispatch(courseAction.deleteCourse(index))
  }
};

export default connect(mapStateToProps, mapDispatchToProps)(App);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
//import './index.css';
import "semantic-ui-css/semantic.css"
import $ from 'jquery';
import App from './App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.min.css'
import configureStore from './store/configureStore';
import { Provider } from 'react-redux';

const store = configureStore();


ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>, 
    document.getElementById('root')   
);

serviceWorker.unregister();
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

非常感谢你的帮助。我也会尽力自己解决这个问题。

请查看以下链接内容:https://dev59.com/s1gR5IYBdhLWcg3wC5mn - 3960278
4个回答

6
import { Header } from 'semantic-ui-react'

将semantic-ui-css更改为semantic-ui-react,因为此库不依赖于jQuery。


2

将CDN添加到位于public文件夹内的index.html头部末尾,并从App.jsindex.js中删除它。

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
</head>

App.js 中,你需要执行以下操作:

import $ from 'jquery';
window.jQuery = $;
window.$ = $;
global.jQuery = $;
import { Header } from 'semantic-ui-css'

希望这能有所帮助。

最好完全不在React中使用jQuery(http://laithazer.com/blog/2017/05/13/how-to-use-jquery-in-react-please-dont/)。我建议使用“semantic-ui-react”代替,正如@ArifKhanKhaishagi在他们的答案中指出的那样(https://dev59.com/pbnoa4cB1Zd3GeqPWsI4#65481727)。 - shaedrich

2
如果您使用以下命令安装semantic-ui:
npm install semantic-ui-css

在你的index.js文件中使用以下导入

import 'semantic-ui-css/semantic.min.css';

它将会消除你在React或其他JavaScript中的错误。


虽然这样可能起作用,但建议遵循最受欢迎的答案所提出的使用“semantic-ui-react”。 - Kerwin Sneijders

0

我找到了一个解决方法 :) 我在index.html中链接了语义化的代码。 <link rel="stylesheet" href="./node_modules/semantic-ui-css/semantic.min.css"> 然后在我的组件中调用了import { Button } from 'semantic-ui-react'; :) 这可能不是最好的方法,因为如果应用程序托管在服务器上,则最好使用CDN,如果像我一样在本地使用它与电子设备一起使用,则可以使用我的方法或单独下载semantic-ui,然后使用link:href。


至少这提供了一个解决方案,虽然不完美但还不错。 - Profnird

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接