Vue+Webpack完美整合富文本編輯器TinyMce的方法
選擇一個(gè)合適的富文本編輯器對(duì)于一個(gè)前端項(xiàng)目至關(guān)重要,這次我基于Vue來(lái)開(kāi)發(fā)我項(xiàng)目中的前端部分,經(jīng)過(guò)權(quán)衡選擇了tinymce。其在UI,功能都很適合,tinymce官方文檔:點(diǎn)擊打開(kāi)鏈接;
引入tinymce 我選用的版本4.7.4
npm install tinymce -S
將tinymce創(chuàng)建為Vue的組件,便于日后復(fù)用,創(chuàng)建組件editor.vue
<template>
<textarea :id="id" :value="value"></textarea>
</template>
<script>
// Import TinyMCE
import tinymce from 'tinymce/tinymce';
import 'tinymce/themes/modern/theme';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/link';
const INIT = 0;
const CHANGED = 2;
var EDITOR = null;
export default {
props: {
value: {
type: String,
required: true
},
setting: {}
},
watch: {
value: function (val) {
console.log('init ' + val)
if (this.status == INIT || tinymce.activeEditor.getContent() != val){
tinymce.activeEditor.setContent(val);
}
this.status = CHANGED
}
},
data: function () {
return {
status: INIT,
id: 'editor-'+new Date().getMilliseconds(),
}
},
methods: {
},
mounted: function () {
const _this = this;
const setting =
{
selector:'#'+_this.id,
language:"zh_CN",
init_instance_callback:function(editor) {
EDITOR = editor;
console.log("Editor: " + editor.id + " is now initialized.");
editor.on('input change undo redo', () => {
var content = editor.getContent()
_this.$emit('input', content);
});
},
plugins:[]
};
Object.assign(setting,_this.setting)
tinymce.init(setting);
},
beforeDestroy: function () {
tinymce.get(this.id).destroy();
}
}
</script>
在鉤子mounted 進(jìn)行了tinymce的初始化工作,調(diào)用 tinymce.init(setting),setting為配置信息這樣我們便初步配置完成了editor組件
在其他頁(yè)面使用組件
<template>
<div class="app-container">
<div>
<!-- 組件有兩個(gè)屬性 value 傳入內(nèi)容雙向綁定 setting傳入配置信息 -->
<editor class="editor" :value="content" :setting="editorSetting" @input="(content)=> content = content"></editor>
</div>
</div>
</template>
<script>
import editor from '@/components/editor'
export default {
name: "editor-demo",
data: function () {
return {
content:'我是富文本編輯器的內(nèi)容',
//tinymce的配置信息 參考官方文檔 https://www.tinymce.com/docs/configure/integration-and-setup/
editorSetting:{
height:400,
}
}
},
components:{
'editor':editor
}
}
</script>
<style scoped>
</style>
此刻我們已經(jīng)完成了百分之90的配置 ,最后只需將node_modules/_tinymce@4.7.4@tinymce/文件夾下的skins文件夾放置于項(xiàng)目根目錄下,這樣tinymce才可獲取皮膚css文件
下載并解壓語(yǔ)言包langs文件夾放置項(xiàng)目根目錄,中文下載鏈接 ,其他語(yǔ)言包選擇;
之后在頁(yè)面輕松使用組件

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
手寫(xiě)實(shí)現(xiàn)vue2下拉菜單dropdown組件實(shí)例
這篇文章主要為大家介紹了手寫(xiě)vue2下拉菜單dropdown組件實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Element?Plus組件Form表單Table表格二次封裝的完整過(guò)程
一般在后臺(tái)管理系統(tǒng)的開(kāi)發(fā)中,都會(huì)遇到很多table,但每一次都去引入el-table就會(huì)導(dǎo)致代碼十分冗余,所以基于組件做一下二次封裝成自己需要的組件就十分nice,下面這篇文章主要給大家介紹了關(guān)于Element?Plus組件Form表單Table表格二次封裝的相關(guān)資料,需要的朋友可以參考下2022-09-09
vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼
今天小編就為大家分享一篇vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue的url請(qǐng)求圖片的問(wèn)題及請(qǐng)求失敗解決
這篇文章主要介紹了vue的url請(qǐng)求圖片的問(wèn)題及請(qǐng)求失敗解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
vue composition-api 封裝組合式函數(shù)的操作方法
在 Vue 應(yīng)用的概念中,“組合式函數(shù)”(Composables) 是一個(gè)利用 Vue 的組合式 API 來(lái)封裝和復(fù)用有狀態(tài)邏輯的函數(shù),這篇文章主要介紹了vue composition-api 封裝組合式函數(shù)的操作方法,需要的朋友可以參考下2022-10-10
vue element插件this.$confirm用法及說(shuō)明(取消也可以發(fā)請(qǐng)求)
這篇文章主要介紹了vue element插件this.$confirm用法及說(shuō)明(取消也可以發(fā)請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

