首页 > 其他分享 >富文本 vue-quill-editor 用法

富文本 vue-quill-editor 用法

时间:2022-10-13 15:26:33浏览次数:51  
标签:vue title value Choice item editor ql quill

1.下载

npm install vue-quill-editor --save

2.在main.js中引入----全局引入

//引入富文本
import quillEditor from 'vue-quill-editor'
// 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(quillEditor, /* { 默认全局 } */)

在页面中单独引入

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 {
  components: {quillEditor}
}

 3.封装成组件,方便多次引用统一配置

<template>
  <div class="vue-quill-editor">
    <quill-editor
      v-model="editorContent"
      ref="myQuillEditor"
      :options="editorOption"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @change="onEditorChange($event)">
    </quill-editor>
  </div>
</template>

<script>
export default {
  props: ['catchData', 'content'],
  data() {
    return {
      editorContent: '', //双向数据绑定数据
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
            ['blockquote', 'code-block'], //引用,代码块
            [{ 'header': 1 }, { 'header': 2 }], // 标题,键值对的形式;1、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', 'video'] //上传图片、上传视频
          ]
        },
        placeholder: '输入内容...'
      }, //编辑器配置项
    }
  },
  watch: {
    //获取数据反显编译器
    content() {
      console.log(this.content,'this.content')
      this.editorContent = this.content
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.setTitleConfig()
    })
  },
  methods: {
    // 失去焦点触发事件
    onEditorBlur() {},
    // 获得焦点触发事件
    onEditorFocus() {},
    // 内容改变触发事件
    onEditorChange(val) {
      console.log(this.editorContent,'this.editorContent')
      this.catchData(this.editorContent)
    },
    //设置工具栏中文提示
    setTitleConfig() {
      // toolbar标题
      const titleConfig = [
        { Choice: '.ql-insertMetric', title: '跳转配置' },
        { Choice: '.ql-bold', title: '加粗' },
        { Choice: '.ql-italic', title: '斜体' },
        { Choice: '.ql-underline', title: '下划线' },
        { Choice: '.ql-header', title: '段落格式' },
        { Choice: '.ql-strike', title: '删除线' },
        { Choice: '.ql-blockquote', title: '块引用' },
        { Choice: '.ql-code', title: '插入代码' },
        { Choice: '.ql-code-block', title: '插入代码段' },
        { Choice: '.ql-font', title: '字体' },
        { Choice: '.ql-size', title: '字体大小' },
        { Choice: '.ql-list[value="ordered"]', title: '编号列表' },
        { Choice: '.ql-list[value="bullet"]', title: '项目列表' },
        { Choice: '.ql-direction', title: '文本方向' },
        { Choice: '.ql-header[value="1"]', title: 'h1' },
        { Choice: '.ql-header[value="2"]', title: 'h2' },
        { Choice: '.ql-align', title: '对齐方式' },
        { Choice: '.ql-color', title: '字体颜色' },
        { Choice: '.ql-background', title: '背景颜色' },
        { Choice: '.ql-image', title: '图像' },
        { Choice: '.ql-video', title: '视频' },
        { Choice: '.ql-link', title: '添加链接' },
        { Choice: '.ql-formula', title: '插入公式' },
        { Choice: '.ql-clean', title: '清除字体格式' },
        { Choice: '.ql-script[value="sub"]', title: '下标' },
        { Choice: '.ql-script[value="super"]', title: '上标' },
        { Choice: '.ql-indent[value="-1"]', title: '向左缩进' },
        { Choice: '.ql-indent[value="+1"]', title: '向右缩进' },
        { Choice: '.ql-header .ql-picker-label', title: '标题大小' },
        { Choice: '.ql-header .ql-picker-item[data-value="1"]', title: '标题一' },
        { Choice: '.ql-header .ql-picker-item[data-value="2"]', title: '标题二' },
        { Choice: '.ql-header .ql-picker-item[data-value="3"]', title: '标题三' },
        { Choice: '.ql-header .ql-picker-item[data-value="4"]', title: '标题四' },
        { Choice: '.ql-header .ql-picker-item[data-value="5"]', title: '标题五' },
        { Choice: '.ql-header .ql-picker-item[data-value="6"]', title: '标题六' },
        { Choice: '.ql-header .ql-picker-item:last-child', title: '标准' },
        { Choice: '.ql-size .ql-picker-item[data-value="small"]', title: '小号' },
        { Choice: '.ql-size .ql-picker-item[data-value="large"]', title: '大号' },
        { Choice: '.ql-size .ql-picker-item[data-value="huge"]', title: '超大号' },
        { Choice: '.ql-size .ql-picker-item:nth-child(2)', title: '标准' },
        { Choice: '.ql-align .ql-picker-item:first-child', title: '居左对齐' },
        { Choice: '.ql-align .ql-picker-item[data-value="center"]', title: '居中对齐' },
        { Choice: '.ql-align .ql-picker-item[data-value="right"]', title: '居右对齐' },
        { Choice: '.ql-align .ql-picker-item[data-value="justify"]', title: '两端对齐' }
      ]
      for (const item of titleConfig) {
        const tip = document.querySelector('.quill-editor ' + item.Choice)
        if (!tip) continue
        tip.setAttribute('title', item.title)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .vue-quill-editor {
    /deep/.ql-container {
      height: 200px;
    }
  }
</style>

4.在页面中引用

//在页面中引入组件
import quillEditor from './quillEditor'

//注册组件
components: { quillEditor }

//html中使用
<quillEditor :catchData="catchContent":content="editorContent"/>

 

5.页面中赋值方法

catchContent(val) {
   this.editorContent = val
   this.page.content = val
},

 

标签:vue,title,value,Choice,item,editor,ql,quill
From: https://www.cnblogs.com/lb0121/p/16788202.html

相关文章

  • Vite + Vue3 + Pinia + es6 + TypeScript 搭建项目
    vite中文参考文档:​​https://vitejs.cn/guide/#scaffolding-your-first-vite-project​​执行 npminitvite@latest步骤如下图:下载依赖npmi 启动项目:npmrundev......
  • Vue3动态路由
    1、引入router: import{useRouter}from'vue-router'2、定义letrouter=useRouter();3、动态添加路由router.addRoute({name:"users",path:'/User/users'......
  • vue-8 组件
    importVuefrom'vue'//全局部引入importElementUIfrom'element-ui'////按需引入import{Row,Button}from'element-ui'import'element-ui/lib/theme-chalk/i......
  • vue-directive__自定义指令
    vue-directive__自定义指令1.复制/***v-copy*复制某个值至剪贴板*接收参数:string类型/Ref<string>类型/Reactive<string>类型*/importtype{Directive,Direct......
  • Vue动态组件 表格
    Vue组件数据源//这里是HTML内容这里通过下面的引入框架结构把数据源传到框架中还有匹配项<Mytable:configList="configList":configData="configData"></Mytable>......
  • vue table表头固定,内容滚动实现
    先上效果   1<template>2<divclass="simpTable">3<table>4<thead>5<th6v-for="(item,index)oftableHe......
  • vuepress 运行报错 Vue packages version mismatch:
    vuepress运行报错Vuepackagesversionmismatch:D:\vuepress-test>npmrundocs:devnpmWARNconfigglobal`--global`,`--local`aredeprecated.Use`--location......
  • vue上传图片并生成海报
    用到的插件:vue-cropper(裁剪)  vue-canvas-poster(生成海报),最终合成海报调用裁剪插件方法getCropBlob,利用canvas画出海报。需求说明:默认有个海报缩略图展示,有模版可供......
  • Vue3组件间传值
    12种方式1.父组件./father.vue点击查看代码<template><h1>father:</h1><h3>子组件传过来的:{{abc}}</h3><inputtype="text"ref="inp"v-model="msg"......
  • Vue——前端框架
    Vue    Vue快速入门  <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="app">......