如何在没有 html 标记的情况下显示 React 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>
  )}
}

此刻我得到这个: 在此处输入图像描述

非常感谢任何帮助!

原文由 Deelux 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 762
2 个回答

在做了一些研究之后,我找到了答案:

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

       <div dangerouslySetInnerHTML={{__html: this.state.content}}></div>

原文由 Deelux 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

 <ReactQuill
   value={this.state.content}
   readOnly={true}
   theme={"bubble"}
/>

这就是它的样子: 样本图像

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

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

这是它在 泡泡主题 中的样子。 雪主题

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

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

 import 'react-quill/dist/quill.bubble.css'

原文由 iShubhamPrakash 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题