经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue3使用vueup/vue-quill富文本、并限制输入字数的方法处理
来源:jb51  时间:2023/3/15 8:52:45  对本文有异议

vue3中使用富文本实在是踩了太多坑了,大概记录一下笨方法处理,有好的方法多交流

一、效果展示

二、npm

  1. npm install @vueup/vue-quill@alpha --save

三、main.js引入

  1. import { QuillEditor } from '@vueup/vue-quill'
  2. import '@vueup/vue-quill/dist/vue-quill.snow.css';
  3. app.component('QuillEditor',QuillEditor)

四、页面使用

  1. <quill-editor
  2. v-model:content="htmlValue"
  3. ref="myQuillEditor"
  4. contentType="html"
  5. :options="editorOption"
  6. @update:content="onEditorChange($event)">
  7. </quill-editor>
  8. <div class="editor_length">{{ TiLength }}/500</div>
  1. const htmlValue=ref("")
  2. const TiLength =ref(0)
  3. const myQuillEditor=ref(null)
  4. const editorOption=reactive({
  5. theme: "snow", // 主题
  6. // debug: 'info',
  7. placeholder: "请输入其他说明",
  8. modules: {
  9. toolbar: {
  10. container: [
  11. ["bold", "italic", "underline", "strike"],
  12. ["blockquote", "code-block"],
  13. [{ header: 1 }, { header: 2 }],
  14. [{ list: "ordered" }, { list: "bullet" }],
  15. [{ script: "sub" }, { script: "super" }],
  16. [{ indent: "-1" }, { indent: "+1" }],
  17. [{ direction: "rtl" }],
  18. [{ size: ["small", false, "large", "huge"] }],
  19. [{ header: [1, 2, 3, 4, 5, 6, false] }],
  20. [{ color: [] }, { background: [] }],
  21. // [{ font: [] }],
  22. [{ align: [] }]
  23. // ["clean", "link", "image"]
  24. // ['addBtn']
  25. ] // 自定义工具栏选项
  26. }
  27. }
  28. })
  29. const onEditorChange=(e)=>{
  30. // console.log(myQuillEditor.value.getText().length);
  31. // console.log(TiLength.value);
  32. TiLength.value =myQuillEditor.value.getText().length-1
  33. if(TiLength.value>500){
  34. myQuillEditor.value.setText(myQuillEditor.value.getText().slice(0,500))
  35. return
  36. }
  37. }
  38. return{
  39. htmlValue,editorOption,onEditorChange,TiLength,myQuillEditor }

五、总结问题(简单记录一下笨方法处理的)

1、双向绑定:v-model:content(一定加content)

2、change事件:@update:content

3、填写下一个富文本上一个未清空时: myQuillEditor.value.setHTML('')强制清空一下

4、编辑进入的时候:强制设置一下内容;并且延迟设置一下字数

  nextTick(()=>{TiLength.value =myQuillEditor.value.getText().length-1 })

到此这篇关于vue3使用vueup/vue-quill富文本、并限制输入字数的文章就介绍到这了,更多相关vue3使用vueup/vue-quill富文本内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号