当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Vue+Element使用富文本编辑器的示例代码

Vue+Element使用富文本编辑器的示例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

彰武逍遥楼,新华南路租房,飓风浏览器

富文本编辑器在任何项目中都会用到,在element中我们推荐vue-quill-editor组件,现在我就把它提供给大家,希望对大家有用。具体截图如下:


安装编辑器组件

具体方法:npm install vue-quill-editor --save

编写组件

首先我们在components文件夹里创建ue.vue组件,效果图如下:

组件

<!-- 组件代码如下 -->
<template>
 <div>
  <script id="editor" type="text/plain"></script>
 </div>
</template>
<script>
 export default {
  name: 'ue',
  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>

在页面中使用

下面是使用代码

<template>
 <div>
  <el-row class="warp">
   <el-col :span="24" class="warp-breadcrum">
    <el-breadcrumb separator=">">
     <el-breadcrumb-item :to="{path:'/home'}"><b>首页</b></el-breadcrumb-item>
     <el-breadcrumb-item :to="{path: '/aboutus/aboutlist'}">关于我们</el-breadcrumb-item>
     <el-breadcrumb-item>添加关于我们</el-breadcrumb-item>
    </el-breadcrumb>
   </el-col>
<!--
form 组件提供了表单验证的功能,只需要通过 rule 属性传入约定的验证规则,并 form-item 的 prop 属性设置为需校验的字段名即可。具体可以参考官网:http://element.eleme.io/#/zh-cn/component/form
-->
   <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' //调用编辑器
 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,这是表单验证
    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>

以上就是全部代码,谢谢大家,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网