当前位置:   article > 正文

vue中如何使用富文本详细讲解_vue 富文本

vue 富文本

一、cnpm 安装 vue-quill-editor

cnpm install vue-quill-editor
  • 1

二、在main.js中引入

import VueQuillEditor from ‘vue-quill-editor’
// require styles 引入样式
import ‘quill/dist/quill.core.css’
import ‘quill/dist/quill.snow.css’
import ‘quill/dist/quill.bubble.css’
Vue.use(VueQuillEditor)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

三、在模块中引用

<template>
	<div>
        <quill-editor 
            v-model="content" 
            ref="myQuillEditor" 
            :options="editorOption" 
            @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
            @change="onEditorChange($event)">
        </quill-editor>
        <!-- 富文本内容 -->
        <div class="text"></div>
        <button v-on:click="saveHtml">保存</button>
    </div>
</template> 
<script>
    import { quillEditor } from 'vue-quill-editor'
    import $ from 'jquery'//先安装jquery
    export default{
        data(){
            return {
                content:null,
                editorOption:{}
            }
        },
        computed: {
	       editor() {
	            return this.$refs.myQuillEditor.quill;
	        },
	    },
        methods:{
            onEditorBlur(){//失去焦点事件
            },
            onEditorFocus(){//获得焦点事件
            },
            onEditorChange(){//内容改变事件
            	//console.log(this.content)
        		//$('.text').html(this.content)
            },
            saveHtml(event){//点击保存按钮保存
	          alert(this.content);
	        }
        }
    }
</script>    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

在这里插入图片描述
这样引入后你会得到这样一个编辑器在这里插入图片描述
那么你如果不需要那么多的工具栏功能要怎么办呢;应该是通过options来修改但是他的默认值是什么的,这个官方文档 :https://quilljs.com/docs/themes/里面看到了类似的方法 。
在这里插入图片描述
初始值的设置应该是一样的吧
所以我就照着toolbar部分去修改了options

<script>
    import { quillEditor } from 'vue-quill-editor'
    export default{
        data(){
            return {
                content:null,
                editorOption:{
                    modules:{
                        toolbar:[
                          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                           ['blockquote', 'code-block']
                        ]
                    }
                }
            }
        },
        methods:{
            onEditorBlur(){//失去焦点事件
            },
            onEditorFocus(){//获得焦点事件
            },
            onEditorChange(){//内容改变事件
            }
        }
    }
</script>   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

果然 生效了 只显示了我写在toolbar里面的模块
在这里插入图片描述
那么toolbar工具栏对应功能的模块名是什么呢 我继续往下看文档 发现大致上有以下的功能

背景颜色 - background
加粗- bold
颜色 - color
字体 - font
内联代码 - code
斜体 - italic
链接 - link
大小 - size
删除线 - strike
上标/下标 - script
下划线 - underline
引用- blockquote
标题 - header
缩进 - indent
列表 - list
文本对齐 - align
文本方向 - direction
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

然而我试着直接引入发现有部分的图标并没有显示;
然后我发现他有些如list这种列表应该是有默认值,我在很后面的文档里发现了这个默认格式规范 这个官方文档也是个坑 内容不写到一块的
在这里插入图片描述
.只需要填写功能名的
加粗 - bold;
斜体 - italic
下划线 - underline
删除线 - strike
引用- blockquote
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean
这一类的引用 直接[‘name’,‘name’]这种格式就好了

2.需要有默认值的
标题 - header
[{ ‘header’: 1 }, { ‘header’: 2 }] 值字体大小

列表 - list
[{ ‘list’: ‘ordered’}, { ‘list’: ‘bullet’ }], 值ordered,bullet

上标/下标 - script
[{ ‘script’: ‘sub’}, { ‘script’: ‘super’ }], 值sub,super

缩进 - indent
[{ ‘indent’: ‘-1’}, { ‘indent’: ‘+1’ }], 值-1,+1等

文本方向 - direction
[{ ‘direction’: ‘rtl’ }], 值rtl
这部分如图所示会填写的内容对应提供的值展示功能的图标 如果多个值他家就显示多个图标
在这里插入图片描述
3.有多个值 以下拉列表形式显示

大小 - size
[{ ‘size’: [‘small’, false, ‘large’, ‘huge’] }],

标题 - header
[{ ‘header’: [1, 2, 3, 4, 5, 6, false] }],
这部分显示如图所示 以下拉列形式显示
在这里插入图片描述
4.有系统默认值的功能只需填写一个空数组 就会有出现默认的选项
颜色 - color
背景颜色 - background
字体 - font
文本对齐 - align
他们的格式都是
[{ ‘color’: [] }, { ‘background’: [] }],
[{ ‘font’: [] }],
[{ ‘align’: [] }]
在这里插入图片描述
toolbar自定义工具栏就是这样咯 剩下的就是根据填写功能到options的modules里就可以了

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']    //上传图片、上传视频

          ]
        },
        theme:'snow'
      }
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/733892
推荐阅读
相关标签
  

闽ICP备14008679号