【问题标题】:Slate: push to database and show in webSlate:推送到数据库并在 Web 中显示
【发布时间】:2019-02-06 02:04:15
【问题描述】:

如何使用 slate.js 将编辑器的内容推送到数据库,如何获取该内容以显示在我的网页上?

【问题讨论】:

    标签: reactjs web frameworks slatejs


    【解决方案1】:

    您应该查看此文档:https://docs.slatejs.org/walkthroughs/saving-to-a-database

    import { Editor } from 'slate-react'
    import Plain from 'slate-plain-serializer'
    ​
    const existingValue = localStorage.getItem('content')
    const initialValue = Plain.deserialize(
      existingValue || 'A string of plain text.'
    )
    ​
    class App extends React.Component {
      state = {
        value: initialValue,
      }
    ​
      onChange = ({ value }) => {
        if (value.document != this.state.value.document) {
          const content = Plain.serialize(value)
    
          //HERE YOU SAVE TO DB
        }
    ​
        this.setState({ value })
      }
    ​
      render() {
        return <Editor value={this.state.value} onChange={this.onChange} />
      }
    }
    

    //在这里你保存到数据库 我是这样保存的:

    let res = await fetch('/save', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        data: content
      })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2020-07-05
      • 2013-08-29
      • 1970-01-01
      相关资源
      最近更新 更多