Vue+Webpack完美整合富文本編輯器TinyMce的方法
選擇一個(gè)合適的富文本編輯器對(duì)于一個(gè)前端項(xiàng)目至關(guān)重要,這次我基于Vue來開發(fā)我項(xiàng)目中的前端部分,經(jīng)過權(quán)衡選擇了tinymce。其在UI,功能都很適合,tinymce官方文檔:點(diǎn)擊打開鏈接;
引入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)文章
vuex如何在非組件中調(diào)用mutations方法
這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue項(xiàng)目history模式刷新404問題解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目history模式刷新404問題的解決辦法,需要的朋友可以參考下2023-11-11
vue前端實(shí)現(xiàn)login頁(yè)登陸驗(yàn)證碼代碼示例
現(xiàn)在我們管理后臺(tái)有個(gè)需求,就是登錄頁(yè)面需要獲取驗(yàn)證碼,用戶可以輸入驗(yàn)證碼后進(jìn)行登錄,這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)login頁(yè)登陸驗(yàn)證碼的相關(guān)資料,需要的朋友可以參考下2024-05-05
vue.js提交按鈕時(shí)進(jìn)行簡(jiǎn)單的if判斷表達(dá)式詳解
這篇文章主要給大家介紹了關(guān)于vue.js提交按鈕時(shí)如何進(jìn)行簡(jiǎn)單的if判斷表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
vue使用腳手架vue-cli創(chuàng)建并引入自定義組件
這篇文章介紹了vue使用腳手架vue-cli創(chuàng)建并引入自定義組件的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼詳解
在使用elementUI el-form 中,對(duì)于業(yè)務(wù)不同的時(shí)候可能會(huì)產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個(gè)表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動(dòng)態(tài)驗(yàn)證的實(shí)例代碼,需要的朋友可以參考下2019-05-05
vue實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示隱藏
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示隱藏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

