前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ckeditor4与vue集成

ckeditor4与vue集成

作者头像
ianzhi
发布2019-07-31 12:51:49
3.5K0
发布2019-07-31 12:51:49
举报

公司的项目开始使用的是ckeditor5,由于ckeditor5目前功能还不够完善,所以不得不替换成ckeditor4,以下记录一下vue-cli项目与ckeditor的集成方法。

下载要使用的ckeditor4构建版本

下载地址:(偶尔会被墙,偶尔不会,建议访问外国网站连接)官网下载地址

根据自己的需要下载,之后解压放到vue的static目录中。

加载ckeditor

修改vue项目中的index.html

<!DOCTYPE html> <html>   <head>     <meta charset="utf-8">     <meta name="renderer"  content="webkit">     <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">     <meta name="viewport" content="width=device-width,initial-scale=1.0">     <script src="static/ckeditor/ckeditor.js"></script>     <title>后台管理系统</title>   </head>   <body>     <div id="app"></div>     <!-- built files will be auto injected -->   </body> </html>

复制以下内容,作为一个vue组件

<template>     <div>         <textarea :id="id"></textarea>     </div> </template> <script> export default {   name: 'ckeditor4',   props: [     'value'   ],   mounted: function() { const self = this // 渲染编辑器     self.ckeditor = window.CKEDITOR.replace(self.id)     // 设置初始内容     self.ckeditor.setData(self.value)     // 监听内容变更事件     self.ckeditor.on('change', function() {       self.$emit('input', self.ckeditor.getData())     })   },   data: function() { return {       id: parseInt(Math.random() * 10000).toString(),       ckeditor: null }   },   watch: {     // 监听prop的变化,更新ckeditor中的值     value: function() { if (this.value !== this.ckeditor.getData()) { this.ckeditor.setData(this.value)       }     }   },   // 销毁组件前,销毁编辑器   beforeDestroy: function() {     self.ckeditor.destroy()   } } </script>

使用组件

首先,引入组件

import ckeditor4 from 'path/to/CkeditorComponent'

添加组件:

... components: {   'ckeditor4': ckeditor4 }, ...

在模板中使用组件:

<ckeditor4 v-model="content"></ckeditor4>

这样,刷新页面,ckeditor4就与vue集成好了。相对于ckeditor5,目前ckeditor4基本包含了对于富文本编辑器的所有需求。鉴于百度ueditor已经不再维护,应该说ckeditor4还是一款非常不错的富文本编辑器。

文章作者ianzhi,原文地址:https://cloud.tencent.com/developer/article/1476872

文章版权归作者所有,转载请保留此声明。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下载要使用的ckeditor4构建版本
  • 加载ckeditor
  • 复制以下内容,作为一个vue组件
  • 使用组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档