简体   繁体   English

导入Quill to React app会抛出“React is defined defined”,“Unexpected token import”

[英]Importing Quill to React app throws “React is not defined”, “Unexpected token import”

I'm trying to get Quill to work on my React app but depending on my webpack config it throws two errors: 我试图让Quill在我的React应用程序上运行但是根据我的webpack配置它会抛出两个错误:

Uncaught SyntaxError: Unexpected token import 未捕获的SyntaxError:意外的令牌导入

or 要么

Uncaught ReferenceError: React is not defined 未捕获的ReferenceError:未定义React

Please note that I'm not using react-quill nor create-react-app. 请注意,我没有使用react-quill和create-react-app。

Solving the first error produces another, I've read that I need to make an exception in webpack to allow it to import from quill folder. 解决第一个错误会产生另一个错误,我已经读过我需要在webpack中进行异常以允许它从quill文件夹导入。

exclude: /node_modules/

into

/node_modules(?!\/quill)/

Now it throws the second error. 现在它抛出了第二个错误。

My webpack config file: 我的webpack配置文件:

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {   
    loaders: [{
      exclude: /node_modules(?!\/quill)/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

I'm using basic code from Quill docs to import what's needed: 我正在使用Quill文档中的基本代码来导入所需的内容:

import React, { Component } from 'react';

import Quill from 'quill/core';

import Toolbar from 'quill/modules/toolbar';
import Snow from 'quill/themes/snow';

import Bold from 'quill/formats/bold';
import Italic from 'quill/formats/italic';
import Header from 'quill/formats/header';

Quill.register({
  'modules/toolbar': Toolbar,
  'themes/snow': Snow,
  'formats/bold': Bold,
  'formats/italic': Italic,
  'formats/header': Header
});
class Tutorial extends Component {
    constructor(props){
        super(props);
        this.editor = null;
    }
    render(){
        return (
            <div className="verb-container">
                <div className="editor"></div>
            </div>
        );
    }
}

export default Tutorial;

When I import just: 当我导入时:

import Quill from 'quill';

or 要么

import Quill from 'quill/core';

A basic editor appears if I initialize it like this: 如果我像这样初始化它,则会出现一个基本编辑器:

this.editor = new Quill('.editor'); this.editor = new Quill('。editor');

But when I try to import 但是当我尝试导入时

import Snow from 'quill/themes/snow';

It shows: 表明:

React is not defined React未定义

If I missed any important information, please let me know. 如果我错过了任何重要信息,请告诉我。

Your question helped me figure out how to get Quill working in a React app, so thank you for that! 你的问题帮助我弄清楚如何让Quill在React应用程序中工作,所以谢谢你!

The "React is not defined" error may have nothing to do with Quill. “React未定义”错误可能与Quill无关。

Possible solution here: Uncaught ReferenceError: React is not defined 可能的解决方案: 未捕获的ReferenceError:未定义React

For what it's worth, here is how I imported Quill into a React component. 对于它的价值,这里是我如何将Quill导入React组件。

Note this is using Quill in a React app, and not using the react-quill npm package. 请注意,这是在React应用程序中使用Quill,而不是使用react-quill npm包。

import React from 'react';

// import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';

import Quill from 'quill/core';
import Toolbar from 'quill/modules/toolbar';
import Snow from 'quill/themes/snow'; //snow works, but need to import and register formats, and replace icons...

import Bold from 'quill/formats/bold';
import Italic from 'quill/formats/italic';
import Header from 'quill/formats/header';
import Underline from 'quill/formats/underline';
import Link from 'quill/formats/link';
import List, { ListItem } from 'quill/formats/list';

import Icons from 'quill/ui/icons'; 

export default class QuillEditor extends React.Component {

  componentDidMount() {

    Quill.register({
      'modules/toolbar': Toolbar,
      'themes/snow': Snow,
      'formats/bold': Bold,
      'formats/italic': Italic,
      'formats/header': Header,
      'formats/underline': Underline,
      'formats/link': Link,
      'formats/list': List,
      'formats/list/item': ListItem,
      'ui/icons': Icons
    });

    var icons = Quill.import('ui/icons');
    icons['bold'] = '<i class="fa fa-bold" aria-hidden="true"></i>';
    icons['italic'] = '<i class="fa fa-italic" aria-hidden="true"></i>';
    icons['underline'] = '<i class="fa fa-underline" aria-hidden="true"></i>';
    icons['link'] = '<i class="fa fa-link" aria-hidden="true"></i>';
    icons['clean'] = '<i class="fa fa-eraser" aria-hidden="true"></i>'; 

    var quill = new Quill('#editor', {
      theme: 'snow', //this needs to come after the above, which registers Snow...
      placeholder: "Write something awesome..."
    });
  } //componentDidMount

  render() {
    return (
      <div>
          <div id="QuillEditor-container">
            {/* <!-- Create the editor container --> */}
            <div id="editor">
              <p>Hello World!</p>
              <p>Some initial <strong>bold</strong> text</p>
              <p></p>
            </div>
          </div>
      </div>
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM