如何将选择的内容纳入我的React项目? [英] How can I incorporate Chosen into my React project?

查看:61
本文介绍了如何将选择的内容纳入我的React项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用一个jQuery插件选择.我通过npm安装了jQuery并选择了:

I want to use a jquery plugin Chosen in my project. I installed jQuery and Chosen via npm:

npm i jquery chosen-js -S

这两个库现在都位于我的node_modules文件夹中.不幸的是,我不断收到错误消息:应用程序尝试编译时为ReferenceError: jQuery is not defined.

Both libraries are now in my node_modules folder. Unfortunately I keep getting an error: ReferenceError: jQuery is not defined when the app tries to compile.

在这里我调用选择的库:

Here is where I make a call to the Chosen library:

import React from "react";
import ReactDom from "react-dom";
import $ from "jquery";
import "chosen-js/chosen.css";
import "chosen-js/chosen.jquery.js";

class App extends React.Component {
  componentDidMount() {
    $(this.refs.list).chosen();
  }

  render() {
    return (
      <select ref="list">
        <option>vanilla</option>
        <option>chocolate</option>
        <option>strawberry</option>
      </select>
    );
  }
}

ReactDom.render(,document.getElementById("app"));

ReactDom.render(, document.getElementById("app"));

下面是错误的图片:

Here's a pic of the error:

推荐答案

不要更改源,最好在组件中编写

don't change sources, better write in your component

window.jQuery = require('jquery');

在选择js之前

这篇关于如何将选择的内容纳入我的React项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆