Vue集成TinyMCE富文本編輯器的流程步驟
前提條件
- 安裝了 Node.js 和 npm。
- 使用 Vue CLI 創(chuàng)建的 Vue 項(xiàng)目。
- 熟悉基本的 Vue 開發(fā)。
1. 創(chuàng)建 Vue 項(xiàng)目
如果你還沒有創(chuàng)建 Vue 項(xiàng)目,可以使用 Vue CLI 來快速搭建一個(gè)新的項(xiàng)目:
npm install -g @vue/cli vue create tinymce-vue-demo cd tinymce-vue-demo
選擇默認(rèn)配置或根據(jù)需要自定義配置(本案例選擇地是 Vue3)。

2.引入 TinyMCE
將下載地 tinymce 包放在 public 目錄下,配置 public/index.html 引入 tinymce.min.js 。


3. 配置 TinyMCE
首先,在你的 Vue 組件中引入并配置 TinyMCE。
<template>
<div id="app">
<h1>TinyMCE in Vue</h1>
<textarea id="editor" v-model="form.content"></textarea>
</div>
</template>
<script>
import { defineComponent, nextTick, onMounted, reactive } from "vue";
export default defineComponent({
setup() {
const form = reactive({
content: "我是初始化內(nèi)容",
});
// 動(dòng)態(tài)引入 TinyMCE
const loadTinyMCE = () => {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = '/tinymce/tinymce.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
};
// 頁(yè)面加載時(shí)
onMounted(() => {
loadTinyMCE().then(() => {
// 富文本編輯器
nextTick(() => {
window.tinymce.init({
selector: '#editor', // 選擇器,指定哪個(gè)元素使用 TinyMCE
license_key: 'gpl', // 許可密鑰,如果是 GPL 版本則不需要設(shè)置
language: 'zh_CN', // 語言設(shè)置
width: '100%', // 編輯器寬度
height: 500, // 編輯器高度
menubar: true, // 是否顯示菜單欄
statusbar: true, // 是否顯示狀態(tài)欄
branding: false, // 去除底部的 TinyMCE 廣告
plugins: [
'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview', 'anchor',
'searchreplace', 'visualblocks', 'code', 'fullscreen', 'insertdatetime', 'media',
'table', 'help', 'wordcount', 'emoticons', 'autosave', 'quickbars', 'codesample'
], // 啟用的插件列表
toolbar: [
'code formatselect fontselect fontsizeselect forecolor backcolor bold italic underline strikethrough link alignment outdent indent bullist numlist blockquote subscript superscript removeformat table image media importword charmap pagebreak formatpainter cut copy undo redo restoredraft searchreplace fullscreen'
], // 工具欄按鈕列表
toolbar_sticky: true, // 工具欄固定在頂部
content_css: '/path/to/content.css', // 自定義內(nèi)容樣式文件路徑
content_style: `
h2 { position: relative; z-index: 99; }
h2::before {
content: "";
display: block;
width: 200px;
height: 8px;
position: absolute;
bottom: 6px;
left: -4px;
z-index: -1;
border-radius: 4px 0 0 4px;
background: linear-gradient(90deg, #F6AFB0 0%, #FFFFFF 100%);
}
`, // 自定義編輯器內(nèi)容的樣式
images_upload_handler: (blobInfo, success, failure) => {
const xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', '/your-backend-endpoint'); // 圖片上傳的后端接口
xhr.onload = () => {
if (xhr.status === 200) {
success(xhr.responseText); // 上傳成功,返回圖片 URL
} else {
failure('HTTP Error: ' + xhr.status); // 上傳失敗,返回錯(cuò)誤信息
}
};
xhr.onerror = () => {
failure('Image upload failed due to a network error.'); // 網(wǎng)絡(luò)錯(cuò)誤
};
xhr.send(blobInfo.blob()); // 發(fā)送圖片數(shù)據(jù)
},
setup: (editor) => {
editor.on('change', () => {
form.content = editor.getContent(); // 監(jiān)聽編輯器內(nèi)容變化并更新表單內(nèi)容
});
}
});
});
}).catch(error => {
console.error('Failed to load TinyMCE:', error); // 處理 TinyMCE 加載失敗的情況
});
});
return {
form
};
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
</style>4. 運(yùn)行項(xiàng)目
確保一切配置正確后,運(yùn)行你的 Vue 項(xiàng)目:
npm run serve
打開瀏覽器訪問 http://localhost:8080,你應(yīng)該能看到一個(gè)帶有 TinyMCE 編輯器的頁(yè)面。

5. 高級(jí)配置與功能
5.1 自動(dòng)保存
自動(dòng)保存用戶的編輯內(nèi)容:
autosave_interval: '5000',
autosave_prefix: 'tinymce-autosave-{path}{query}-{id}-',
autosave_restore_when_empty: true,
autosave_retention: '2m'5.2 拼寫檢查
啟用拼寫檢查功能:
spellchecker_languages: 'English=en,Spanish=es,German=de,French=fr,Italian=it,Portuguese=pt,Brazilian Portuguese=pt_BR', spellchecker_rpc_url: '/your-spellcheck-endpoint'
5.3 自定義樣式
為編輯器內(nèi)容定義自定義的 CSS 樣式:
content_css: '/path/to/your/custom.css'
5.4 實(shí)時(shí)協(xié)作(付費(fèi)功能)
實(shí)時(shí)協(xié)作功能通常是一個(gè)付費(fèi)功能,需要購(gòu)買相應(yīng)的許可證。
結(jié)論
通過以上步驟,你已經(jīng)成功地在 Vue 項(xiàng)目中集成了 TinyMCE 富文本編輯器,并且配置了多種高級(jí)功能。TinyMCE 提供了豐富的功能和靈活的配置選項(xiàng),能夠滿足大多數(shù) Web 應(yīng)用的需求。如果需要更多高級(jí)功能,可以考慮購(gòu)買 TinyMCE 的付費(fèi)版本。希望這篇文章對(duì)你有所幫助!
到此這篇關(guān)于Vue集成TinyMCE富文本編輯器的流程步驟的文章就介紹到這了,更多相關(guān)Vue集成TinyMCE內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue如何解決每次發(fā)版都要強(qiáng)刷清除瀏覽器緩存問題
這篇文章主要介紹了Vue如何解決每次發(fā)版都要強(qiáng)刷清除瀏覽器緩存問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
詳解vue-cli 本地開發(fā)mock數(shù)據(jù)使用方法
這篇文章主要介紹了詳解vue-cli 本地開發(fā)mock數(shù)據(jù)使用方法,如果后端接口尚未開發(fā)完成,前端開發(fā)一般使用mock數(shù)據(jù)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
手把手教你拿捏vue?cale()計(jì)算函數(shù)使用
vue-cli啟動(dòng)本地服務(wù)局域網(wǎng)不能訪問的原因分析
Vue.js第二天學(xué)習(xí)筆記(vue-router)
Vue3中Vite和Vue-cli的特點(diǎn)與區(qū)別詳解
vue-cli構(gòu)建的項(xiàng)目如何手動(dòng)添加eslint配置
在vue里面設(shè)置全局變量或數(shù)據(jù)的方法

