如何在没有 html 标记的情况下显示 React Quill 的内容?

Dee*_*lux 13 javascript reactjs quill

我设法让我的 Quill 工作,但现在我想像我们在这个论坛上那样制作一个漂亮的分屏,但我无法弄清楚的一件事是如何在预览端将 Quill 的输入转换为漂亮的文本.

我能够显示文本,但它仍然包含我当然不想要的所有 html 标签。

所以这是我到目前为止的 Quill 设置:

export default class AddSpark extends Component {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);

    this.state ={
      content: '',
    };
  }

  onChange(html) {
    this.setState ({ content: html });
      console.log(html)
    }

  render() {
    return (
      <div>
      <Col xs={12} md={6}>
        <form ref={(input) => this.sparkForm = input} onSubmit={(e) => this.createSpark(e)}>

            <ControlLabel>Select your city</ControlLabel>
            <select id="formControlsCity" placeholder="Choose your city" onChange={this.onChange} className="form-control" onClick={ moreOptions } ref={(input) => this.city = input}>
              <option value="select">Choose your city</option>
              <option value="Beijing">Beijing</option>
              <option value="Shanghai">Shanghai</option>
              <option value="Chengdu & Chongqing">Chengdu & Chongqing</option>
            </select>
       
            <ControlLabel>Select your person</ControlLabel>
            <select id="formControlsPerson" placeholder="Choose your person" className="form-control" ref={(input) => this.person = input}>
              <option value="select">First select your city</option>
            </select>
    

            <ControlLabel>Select your location</ControlLabel>
            <select id="formControlsLocation" placeholder="Choose your location" className="form-control" ref={(input) => this.location = input}>
              <option value="select">First select your city</option>
            </select>

            <ControlLabel>Title</ControlLabel>
            <input type="text" label="Title" placeholder="Enter your title" className="form-control" ref={(input) => this.title = input}/>
          

            <ControlLabel>Content</ControlLabel>
              <div className='_quill'>
                <ReactQuill
                  ref='editor'
                  onChange={this.onChange}
                />
              </div>
              <br />

          <Button type="submit">Submit</Button>
        </form>
      </Col>
      <Col xs={12} md={6}>
      <h3>Preview</h3>
        {this.state.content}
      </Col>

      </div>
  )}
}
Run Code Online (Sandbox Code Playgroud)

目前我得到这个: 在此处输入图片说明

任何帮助表示高度赞赏!

Dee*_*lux 15

经过一番研究,我找到了答案:

要在没有 html 标签的预览部分显示 Quill 的内容,我使用了以下代码:

      <div dangerouslySetInnerHTML={{__html: this.state.content}}></div>
Run Code Online (Sandbox Code Playgroud)

  • CSS 样式怎么样?HTML 代码中有一些类,不是内联样式的。 (6认同)

iSh*_*ash 15

最简单的方法是在禁用编辑的情况下使用相同的 react-quill 组件。这种方法不需要你安装任何额外的包。您可以通过传递一个属性 readOnly 其值设置为 true (默认情况下为 false)来实现

以下是可用于预览端的组件代码:

<ReactQuill
   value={this.state.content}
   readOnly={true}
   theme={"bubble"}
/>
Run Code Online (Sandbox Code Playgroud)

这是它的样子: 示例图像

注意: ReactQuill 带有两个内置主题(气泡和雪)。

这是它在雪主题中的外观。它在顶部有一个工具栏: 气泡

这是它在气泡主题中的外观。 雪主题

您应该通过在主题道具中传递字符串“bubble”来使用“bubble”主题来显示您的编辑器内容。这是因为它没有像“雪”主题那样顶部的工具栏。

为了使用气泡主题,您还必须为此特定主题导入 CSS 样式表,如下所示:*

import 'react-quill/dist/quill.bubble.css'
Run Code Online (Sandbox Code Playgroud)


小智 7

onChange(content, delta, source, editor) {
  const text = editor.getText(content);
  this.setState ({ content: text });
  console.log(text)
}
Run Code Online (Sandbox Code Playgroud)


Rom*_*nko 7

你需要 html-react-parser

import Parser from 'html-react-parser';

...     // in your render
<ReactQuill
    ...
    value={code} 
    onChange={this.previewCode}
/>
...
{Parser(code)}
Run Code Online (Sandbox Code Playgroud)


小智 5

我在React.js中这样使用它。为了在div中显示quil内容,我使用类“ql-editor”,因为通过这个我们的div可以使用quil默认类...有效..

import ReactQuill from "react-quill";
import "react-quill/dist/quill.snow.css";

const ReactMarkdown = require("react-markdown/with-html"); //for displaying html

function editorContent() {
  <div className="ql-editor" style={{ padding: 0 }}>
        <ReactMarkdown escapeHtml={false} source={quilGeneratedHtml} />
  </div>
}
Run Code Online (Sandbox Code Playgroud)