当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue2.0项目中使用Ueditor富文本编辑器示例代码

vue2.0项目中使用Ueditor富文本编辑器示例代码

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

魔屋电影,神韵在线,都市龙隐

最近在vue项目中需要使用富文本编辑器,于是将ueditor集成进来,作为公共组件。

项目地址: 

1.放入静态资源并配置

首先把官网下载的ueditor资源,放入静态资源src/static中。

 修改ueditor.config.js中的window.ueditor_home_url配置,如下图:

2.引入

在main.js中引入

import '../static/ue/ueditor.config.js'
import '../static/ue/ueditor.all.min.js'
import '../static/ue/lang/zh-cn/zh-cn.js'
import '../static/ue/ueditor.parse.min.js'

3.开发公共组件

开发公共组件,可设置填充内容defaultmsg,配置信息config(宽度和高度等),并提供获取内容的方法。

<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>

4.使用

当我们需要使用富文本编辑器时,直接调用公共组件即可

<template>
 <div class="components-container">
  <div class="info">ue编辑器示例<br>需要使用编辑器时,调用ue公共组件即可。可设置填充内容defaultmsg,配置信息config(宽度和高度等),可调用组件中获取内容的方法。</div>
  <button @click="getuecontent()">获取内容</button>
  <div class="editor-container">
   <ue :defaultmsg=defaultmsg :config=config ref="ue"></ue>
  </div>
 </div>
</template>
<style>
 .info{
  border-radius: 10px;
  line-height: 20px;
  padding: 10px;
  margin: 10px;
  background-color: #ffffff;
 }
</style>
<script>
 import ue from '../../components/ue/ue.vue';
 export default {
  components: {ue},
  data() {
   return {
    defaultmsg: '这里是ue测试',
    config: {
     initialframewidth: null,
     initialframeheight: 350
    }
   }
  },
  methods: {
   getuecontent() {
    let content = this.$refs.ue.getuecontent();
    this.$notify({
     title: '获取成功,可在控制台查看!',
     message: content,
     type: 'success'
    });
    console.log(content)
   }
  }
 };
</script>

效果如下:

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

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

相关文章:

验证码:
移动技术网