Vue.js結(jié)合Ueditor富文本編輯器的實(shí)例代碼
在前端開發(fā)的項(xiàng)目中。難免會遇到需要在頁面上集成一個富文本編輯器。
前一段時間公司Vue.js項(xiàng)目需要使用UEditor富文本編輯器,在百度上搜索一圈沒有發(fā)現(xiàn)詳細(xì)的說明,決定自己嘗試,忙活了一天終于搞定了。
1. 總體思路
1.1 模塊化
vue的很大的一個優(yōu)勢在于模塊化,我們可以通過模塊化實(shí)現(xiàn)頁面和邏輯的復(fù)用。所以可以把Ueditor重新封裝成一個.vue的模板文件。其他組件通過引入這個模板實(shí)現(xiàn)代碼復(fù)用。
1.2 數(shù)據(jù)傳輸
首先父組件需要設(shè)置編輯器的長度、寬度、初始文本,這些數(shù)據(jù)可以通過props來傳遞。編輯器中的文本變化可以通過vue自定義事件向父組件傳遞。
2. 具體實(shí)現(xiàn)步驟
2.1 引入關(guān)鍵的JS以及CSS文件
將以下文件全部拷貝到項(xiàng)目中

2.2 配置Ueditor.config.js
首先配置URL參數(shù),我們需要將這個路徑指向剛才拷貝的文件的更目錄,注意這里最好使用相對路勁。
var URL = window.UEDITOR_HOME_URL || '/static/ueditor/';
然后是默認(rèn)寬度高度的設(shè)置
,initialFrameWidth:null // null表示寬度自動 ,initialFrameHeight:320
其他功能的配置可以在官方文檔查看
2.3 創(chuàng)建編輯器模板
我們需要在編輯器模板中import Ueditor核心JS庫,并添加contentChange回調(diào)函數(shù)就大功告成了。
之所以使用import語法來引入核心JS庫是因?yàn)檫@樣更符合ES6模塊化的規(guī)范,我看到網(wǎng)上有人建議在main.js中引入JS,但是過早地引入JS可能導(dǎo)致頁面首次加載緩慢。
<template>
<div ref="editor"></div>
</template>
<script>
/* eslint-disable */
import '../../../assets/js/ueditor/ueditor.config';
import '../../../assets/js/ueditor/ueditor.all';
import '../../../assets/js/ueditor/lang/zh-cn/zh-cn';
import { generateRandonInteger } from '../../../vuex/utils';
export default {
data() {
return {
id: generateRandonInteger(100000) + 'ueditorId',
};
},
props: {
value: {
type: String,
default: null,
},
config: {
type: Object,
default: {},
}
},
watch: {
value: function value(val, oldVal) {
this.editor = UE.getEditor(this.id, this.config);
if (val !== null) {
this.editor.setContent(val);
}
},
},
mounted() {
this.$nextTick(function f1() {
// 保證 this.$el 已經(jīng)插入文檔
this.$refs.editor.id = this.id;
this.editor = UE.getEditor(this.id, this.config);
this.editor.ready(function f2() {
this.editor.setContent(this.value);
this.editor.addListener("contentChange", function () {
const wordCount = this.editor.getContentLength(true);
const content = this.editor.getContent();
const plainTxt = this.editor.getPlainTxt();
this.$emit('input', { wordCount: wordCount, content: content, plainTxt: plainTxt });
}.bind(this));
this.$emit('ready', this.editor);
}.bind(this));
});
},
};
</script>
<style>
body{
background-color:#ff0000;
}
</style>
3. 編輯器的使用
使用編輯器模板的時候我需要通過props傳入config以及初始文本value。
<template xmlns:v-on="http://www.w3.org/1999/xhtml">
<div class="edit-area">
<ueditor v-bind:value=defaultMsg v-bind:config=config v-on:input="input" v-on:ready="ready"></ueditor>
</div>
</template>
<script>
import ueditor from './ueditor.vue';
export default {
components: {
ueditor,
},
data() {
return {
defaultMsg: '初始文本',
config: {
initialFrameWidth: null,
initialFrameHeight: 320,
},
};
},
};
</script>
如果需要讓Ueditor上傳圖片的話,還需要在后臺配置一個接口。這部分還沒有時間研究,等過幾天補(bǔ)上
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js 公式編輯器 - 自定義匹配規(guī)則 - 帶提示下拉框 - 動態(tài)獲取光標(biāo)像素坐標(biāo)
- 不到200行 JavaScript 代碼實(shí)現(xiàn)富文本編輯器的方法
- 使用JavaScript實(shí)現(xiàn)表格編輯器(實(shí)例講解)
- JSP實(shí)用教程之簡易頁面編輯器的實(shí)現(xiàn)方法(附源碼)
- JS模仿編輯器實(shí)時改變文本框?qū)挾群透叨却笮〉姆椒?/a>
- 分享9個最好用的JavaScript開發(fā)工具和代碼編輯器
- node.js集成百度UE編輯器
- Javascript實(shí)現(xiàn)簡單的富文本編輯器附演示
- javascript獲取ckeditor編輯器的值(實(shí)現(xiàn)代碼)
- 4個頂級JavaScript高級文本編輯器
相關(guān)文章
Vue數(shù)據(jù)雙向綁定底層實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Vue數(shù)據(jù)雙向綁定底層實(shí)現(xiàn)原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11
Vue學(xué)習(xí)筆記進(jìn)階篇之函數(shù)化組件解析
本篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之函數(shù)化組件探究,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
vue項(xiàng)目中vue-i18n和element-ui國際化開發(fā)實(shí)現(xiàn)過程
這篇文章主要介紹了vue項(xiàng)目中vue-i18n和element-ui國際化開發(fā)實(shí)現(xiàn)過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
vue項(xiàng)目在打包時,如何去掉所有的console.log輸出
這篇文章主要介紹了vue項(xiàng)目在打包時,如何去掉所有的console.log輸出,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

