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

时间:2018-04-17 15:15:26

标签: javascript reactjs webpack quill

我正在尝试让Quill使用我的React应用程序但是根据我的webpack配置它会引发两个错误:

  

未捕获的SyntaxError:意外的令牌导入

  

未捕获的ReferenceError:未定义React

请注意,我没有使用react-quill,也没有使用create-react-app。

解决第一个错误会产生另一个错误,我已经读过我需要在webpack中进行异常以允许它从quill文件夹导入。

exclude: /node_modules/

进入

/node_modules(?!\/quill)/

现在它抛出了第二个错误。

我的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: './'
  }
};

我正在使用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;

当我导入时:

import Quill from 'quill';

import Quill from 'quill/core';

如果我像这样初始化它,则会出现一个基本编辑器:

  

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

但是当我尝试导入

import Snow from 'quill/themes/snow';

它显示:

  

未定义React

如果我遗漏了任何重要信息,请告知我们。

1 个答案:

答案 0 :(得分:2)

您的问题帮助我弄清楚了如何使Quill在React应用中正常工作,谢谢您!

“未定义反应”错误可能与Quill无关。

此处可能的解决方案:Uncaught ReferenceError: React is not defined

对于它的价值,这是我如何将Quill导入到React组件中。

请注意,这是在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>
    )
  }
}