1、下载Vue-Quill-Editor
npm install vue-quill-editor --save
2、下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
3、使用富文本编辑器:
vue代码
<template>
<div class="edit_container">
<p>用户名:<input type="text" v-model="name"></p>
<!-- 新增时输入 -->
简介
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
<!-- 从数据库读取展示 -->
<p><button @click="post_info()">提交</button></p>
<div v-html="str" class="ql-editor">
{{str}}
</div>
{{str}}
</div>
</template>
<script>
import axios from 'axios'
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
export default {
name:"fuwenben",
components: {
quillEditor
},
data() {
return {
content: `<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>`,
str: '',
editorOption: {},
name
}
},
methods: {
onEditorReady(editor) { // 准备编辑器
},
onEditorBlur(){}, // 失去焦点事件
onEditorFocus(){}, // 获得焦点事件
onEditorChange(){}, // 内容改变事件
post_info(){
var data_form=new FormData()
data_form.append("name",this.name)
data_form.append("content",this.content)
axios({
url:"http://127.0.0.1:8000/qiniu/test/",
method:"post",
data:data_form
}).then(res=>{
console.log(res.data)
this.str = res.data.data
})
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill;
},
},
}
</script>
django代码
from django.db import models # Create your models here. class Test(models.Model): name = models.CharField(max_length=32) content = models.TextField()
from rest_framework.views import APIView from rest_framework.response import Response from .models import Test class Test1(APIView): def post(self,request): print(request.data) info = Test.objects.create(name=request.data["name"],content=request.data["content"]) last = Test.objects.filter().last() return Response({"code":200,"data":last.content})
4、前端显示富文本编辑的内容
//显示适合HTML的内容 <div v-html="str" class="ql-editor"> {{str}} </div> //显示原文本 {{str}}