前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >element-vue使用富文本编辑器【前端】

element-vue使用富文本编辑器【前端】

作者头像
sinnoo
发布2020-11-13 11:15:17
2.8K0
发布2020-11-13 11:15:17
举报
文章被收录于专栏:技术人生技术人生

一、前言

1.富文本编辑器选择的为vue-quill-editor

官方地址:https://quilljs.com/docs/quickstart/

2.安装

代码语言:javascript
复制
cnpm install vue-quill-editor
代码语言:javascript
复制
cnpm install quill

3.在对应的页面引入,在components中新建Quilleditor.vue

里面代码如下

代码语言:javascript
复制
<!-- 组件代码如下 -->   
<template>              
    <div>
        <script id="editor" type="text/plain"></script>
    </div>
</template>
<script>
export default {        
    name: 'Quilleditor',
    data () {
        return {        
            editor: null
        }
    },
    props: {            
        defaultMsg: {   
            type: String
        },
        config: {       
            type: Object
        }
    },
    mounted() {
        const _this = this;
        this.editor = UE.getEditor('editor', this.config); // 初始化UE
        this.editor.addListener("ready", function () {
            _this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后,放入内容。
        });
    },
    methods: {
        getUEContent() { // 获取内容方法
            return this.editor.getContent()
        }
    },
    destroyed() {
        this.editor.destroy();
    }
}
</script

二、页面中引入

1.views中创建模板auditionadd.vue

代码如下:

代码语言:javascript
复制
<template>
    <div>
        <el-row class="warp">
            <el-col :span="24" class="warp-main">
                <el-form ref="infoForm" :model="infoForm" :rules="rules" label-width="120px">
                    <el-form-item label="标题" prop="a_title">
                        <el-input v-model="infoForm.a_title"></el-input>
                    </el-form-item>
                    <el-form-item label="来源" prop="a_source">
                        <el-input v-model="infoForm.a_source"></el-input>
                    </el-form-item>
                    <el-form-item label="内容">
                        <div class="edit_container">
                            <quill-editor v-model="infoForm.a_content" ref="myQuillEditor" class="editer" :options="editorOption" @ready="onEditorReady($event)">
                            </quill-editor>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">保存</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>
<script>
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 {
    data() {
        return {
            infoForm: {
                a_title: '',
                a_source: '',
                a_content:'',
                editorOption: {}
            },
            //表单验证
            rules: {
                a_title: [
                    {required: true, message: '请输入标题', trigger: 'blur'}
                ],
                a_content: [
                    {required: true, message: '请输入详细内容', trigger: 'blur'}
                ]
            },
        }
    },      
    computed: { 
        editor() {  
            return this.$refs.myQuillEditor.quill
        }           
    },              
    mounted() {         
        //初始化    
    },              
    methods: {          
        onEditorReady(editor) {
        },                  
        onSubmit() {    
            this.$refs.infoForm.validate((valid) => {
                if(valid) {
                    this.$post('m/add/about/us',this.infoForm).then(res => {
                        if(res.errCode == 200) {
                            this.$message({
                                message: res.errMsg,
                                type: 'success'
                            });
                            this.$router.push('/aboutus/aboutlist');
                        } else {
                            this.$message({
                                message: res.errMsg,
                                type:'error'
                            });
                        }
                    });
                }
            });
        }
    },
    components: {
        quillEditor
    }
}
</script>

2.效果图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、页面中引入
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档