vue2-editor富文本编辑器使用

单页应用

实现富文本生成并自定义图片上传

1. 安装

npm install vue2-editor

2. 上代码

template

<vue-editor v-model="content" id="editor" useCustomImageHandler @image-added="handleImageAdded" :editorToolbar="customToolbar">
</vue-editor>

js

//组件中使用
import { VueEditor} from "vue2-editor";
export default {
  components: {
        VueEditor
    },
    data() {
        return {
            content: '',
            customToolbar: [ //默认的工具栏中没有字体大小工具,未能满足使用,所以自定义加上
                ['bold', 'italic', 'underline', 'strike'],
                ['blockquote', 'code-block'],
                [{
                    'header': 1
                }, {
                    'header': 2
                }],
                [{
                    'script': 'sub'
                }, {
                    'script': 'super'
                }], // superscript/subscript
                [{
                    'indent': '-1'
                }, {
                    'indent': '+1'
                }], // outdent/indent
                [{
                    'direction': 'rtl'
                }],
                [{
                    'header': [1, 2, 3, 4, 5, 6, false]
                }],
                [{
                    'size': ['small', false, 'large', 'huge']
                }],
                [{
                    'color': []
                }, {
                    'background': []
                }], // dropdown with defaults from theme
                [{
                    'font': []
                }],
                [{
                    'align': []
                }],
                ['image'],
                ['clean']
            ]
        }
    },
    methods: {
        handleImageAdded: function(file, Editor, cursorLocation, resetUploader) {
            //上传图片之后,拿到url,加入到内容中去
            let url = 'http://图片地址';
            Editor.insertEmbed(cursorLocation, "image", url);
        }
    }
}

3.查看成果

<!DOCTYPE html>
<html>
    <head>
     <!-- 注意加上该样式,不然字体size那些类不生效 -->
        <link href="http://cdn.staticfile.org/quill/1.3.6/quill.core.css" rel="stylesheet">
       
    </head>
    <body class="ql-editor">
        <%- content %>
    </body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容