当前位置:   article > 正文

VUE- elementUI使用quill富文本编辑器(编辑文本、上传图片)

elementui编辑器

准备工作:安装  yarn install vue-quill-editor

  1. main.js
  2. // 编辑器
  3. import VueQuillEditor from 'vue-quill-editor'
  4. // 引入样式
  5. import 'quill/dist/quill.core.css'
  6. import 'quill/dist/quill.snow.css'
  7. import 'quill/dist/quill.bubble.css'
  8. // 需要在new VUE之前
  9. Vue.use(VueQuillEditor);
  1. test.vue
  2. <template>
  3. <div>
  4. <!-- // 新增按钮-->
  5. <div style="width: 100%; text-align: right;">
  6. <el-button type="success" style="float: left" @click="addCommodity">新增商品</el-button>
  7. </div>
  8. <br><br>
  9. <div>
  10. <el-dialog title="新增商品信息" :visible.sync="dialogFormVisible" width="50%" :close-on-click-modal="false">
  11. <el-form-item label="商品介绍" :rules="[{ required: true, message: '请输入商品介绍', trigger: 'blur' }]"><br>
  12. <quill-editor
  13. v-model="content"
  14. :options="editorOption"
  15. @blur="onEditorBlur($event)"
  16. @focus="onEditorFocus($event)"
  17. @change="onEditorChange($event)">
  18. </quill-editor>
  19. </el-form-item>
  20. </el-form>
  21. <div slot="footer" class="dialog-footer">
  22. <el-button @click="dialogFormVisible = false">取 消</el-button>
  23. <el-button type="primary" @click="handleAdd">确 定</el-button>
  24. </div>
  25. </el-dialog>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import {quillRedefine} from 'vue-quill-editor-upload'
  31. export default {
  32. name: "CommodityList",
  33. data() {
  34. return {
  35. dialogFormVisible: false,
  36. formLabelWidth: 200,
  37. form: {
  38. xuni: 0,
  39. },
  40. content: '',
  41. uploadUrl: this.$webSite + "/manage/upload_api/",
  42. }
  43. },
  44. created() {
  45. this.editorOption = quillRedefine(//修改富文本编辑器图片上传路径
  46. {
  47. // 图片上传的设置
  48. uploadConfig: {
  49. action: this.uploadUrl, // 必填参数 图片上传地址\
  50. too: [['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{'header': 1}, {'header': 2}], [{'list': 'ordered'}, {'list': 'bullet'}], [{'script': 'sub'}, {'script': 'super'}], [{'indent': '-1'}, {'indent': '+1'}], [{'direction': 'rtl'}], [{'size': ['small', false, 'large', 'huge']}], [{'header': [1, 2, 3, 4, 5, 6, false]}], [{'color': []}, {'background': []}], [{'font': []}], [{'align': []}], ['clean'], ['image']],
  51. header: (xhr) => {
  52. //关键是这句话
  53. xhr.setRequestHeader('Authorization', window.localStorage.getItem('token'));
  54. xhr.setRequestHeader('Username', window.localStorage.getItem('userid'));
  55. return xhr
  56. },
  57. res: (response) => {
  58. return response.url;//return图片url
  59. },
  60. accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon',
  61. name: 'file' // 图片上传参数名
  62. }
  63. })
  64. this.editorOption.modules = {
  65. toolbar: [
  66. ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
  67. ['blockquote', 'code-block'], //引用,代码块
  68. [{'header': 1}, {'header': 2}], // 标题,键值对的形式;12表示字体大小
  69. [{'list': 'ordered'}, {'list': 'bullet'}], //列表
  70. [{'script': 'sub'}, {'script': 'super'}], // 上下标
  71. [{'indent': '-1'}, {'indent': '+1'}], // 缩进
  72. [{'direction': 'rtl'}], // 文本方向
  73. [{'size': ['small', false, 'large', 'huge']}], // 字体大小
  74. [{'header': [1, 2, 3, 4, 5, 6, false]}], //几级标题
  75. [{'color': []}, {'background': []}], // 字体颜色,字体背景颜色
  76. [{'font': []}], //字体
  77. [{'align': []}], //对齐方式
  78. ['clean'], //清除字体样式
  79. ['image'] //上传图片、上传视频
  80. ],
  81. }
  82. this.editorOption.placeholder = "请输入商品描述"
  83. },
  84. methods: {
  85. addCommodity() {
  86. this.dialogFormVisible = true;
  87. }
  88. }
  89. }
  90. </script>
  91. <style scoped>
  92. </style>
  1. views.py
  2. class ImageArticleView(APIView):
  3. @check_role
  4. def post(self, request):
  5. file_data = request.FILES['file'] # 获取上传的文件数据
  6. file_type = '.' + file_data.name.split('.')[-1]
  7. tmp_data = uid()
  8. file_data.name = tmp_data + file_type
  9. file_path = 'http://127.0.0.1:8000' + '/media/image_path/' + file_data.name
  10. image = ImageUpload(
  11. sid=tmp_data,
  12. image=file_data,
  13. account=account(request)
  14. )
  15. image.save()
  16. response_data = {'file_name': file_data.name, 'url': file_path}
  17. print('upload response: ', response_data)
  18. return JsonResponse(response_data)

至此,只要点击图片并上传后,图片会插入到编辑器当中,以URL的方式进行保存。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/544379
推荐阅读