vue3使用vueup/vue-quill富文本、并限制输入字数

本文记录了在Vue3项目中集成vue-quill富文本编辑器的过程,包括npm安装、main.js引入、组件使用以及遇到的如双向绑定、内容更新和字数限制等问题的解决方法。作者提供了具体的代码示例和注意事项。
摘要由CSDN通过智能技术生成

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

目录

一、效果展示

二、npm

三、main.js引入

四、页面使用

五、总结问题

文档地址:Events | VueQuill

一、效果展示

二、npm

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

三、main.js引入

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

四、页面使用

<quill-editor
 v-model:content="htmlValue"
 ref="myQuillEditor"
 contentType="html"
 :options="editorOption"
 @update:content="onEditorChange($event)">
</quill-editor>
<div class="editor_length">{{ TiLength }}/500</div>
      const htmlValue=ref("")
      const TiLength =ref(0)
      const myQuillEditor=ref(null)
      const editorOption=reactive({
        theme: "snow", // 主题    
        // debug: 'info',
        placeholder: "请输入其他说明",
        modules: {
          toolbar: {
            container: [
              ["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", "link", "image"]
              // ['addBtn']
            ] // 自定义工具栏选项
          }
        }
      })
      const onEditorChange=(e)=>{
        // console.log(myQuillEditor.value.getText().length);
        // console.log(TiLength.value);
        TiLength.value =myQuillEditor.value.getText().length-1
        if(TiLength.value>500){
          myQuillEditor.value.setText(myQuillEditor.value.getText().slice(0,500))
          return
          }
      }
        
        return{
        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 })

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值