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}}
BZUFXXHAEPRDEETY
mpvwlvuigfrcbefi tyxudayvidgcbchf