vue集成kindeditor富文本

指令

该指令的作用是dom衬着后触发,由于非vue的插件有的是dom必需存在的情况下才能够实行

Vue.directive('loaded-callback', {
  inserted: function (el, binding, vnode) {
    binding.value(el, binding, vnode)
  }
})

装置kindeditor

npm install kindeditor

kindeditor组件

<template>
  <div class="kindeditor">
    <textarea class="form-control" ref="kindeditor" v-model="localValue" name="content" v-loaded-callback='initKindeditor'></textarea>
  </div>
</template>

<script>
import '../../../../../node_modules/kindeditor/kindeditor-all.js'
import '../../../../../node_modules/kindeditor/lang/zh-CN.js'
import '../../../../../node_modules/kindeditor/themes/default/default.css'
export default {
  name: 'kindeditor',
  props: ['options', 'value'],
  data () {
    return {
      localValue: ''
    }
  },
  watch: {
    localValue: 'updateValue',
    value: 'setDefault'
  },
  created () {
    this.setDefault()
  },
  methods: {
    initKindeditor () {
        var _this = this
        // 默许参数
        var options = {
            uploadJson: 'upload/image',
            width: '100%',
            afterChange () {
                _this.localValue = this.html()
            }
        }
        // 挪用外部参数
        if (_this.options) {
            for(var i in _this.options){
                options[i] = _this.options[i]
            }
        }
        KindEditor.create(_this.$refs.kindeditor,options);
    },
    // 设置初始值
    setDefault () {
      this.localValue = this.value
    },
    // 修正父件的值
    updateValue () {
      this.$emit('input',this.localValue)
    }
  }
}
</script>

用法

<kindeditor :options="options" v-model="content"></kindeditor>

options参考

http://kindeditor.net/docs/op…

    原文作者:改改心情
    原文地址: https://segmentfault.com/a/1190000019408320
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞