2

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

4

1 回答 1

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
  })
})
于 2018-12-27T18:48:21.617 回答