vue.js組件化使用百度富文本編輯器(一)

注意:html

本文采用的編輯器爲:ideavue

1.下載百度富文本編輯器,地址:https://ueditor.baidu.com/website/download.html#ueditorweb

2.加入到static文件夾下,如圖:npm

3.在main.js中引入js。編輯器

注意:必定要修改ueditor.config.js中的路徑ide

window.UEDITOR_HOME_URL = "./static/ueditor/"

 

 

4.編寫vue組件:學習

<template>
<div>
<script id="editor" type="text/plain"></script>
</div>
</template>
<script>
export default {
name: 'UE',
data () {
return {
editor: null
}
},
props: {
defaultMsg: {
type: String
},
config: {
type: Object
}
},
mounted() {
const _this = this;
this.editor = UE.getEditor('editor', this.config); // 初始化UE
this.editor.addListener("ready", function () {
_this.editor.setContent(_this.defaultMsg); // 確保UE加載完成後,放入內容。
});
},
methods: {
getUEContent() { // 獲取內容方法
return this.editor.getContent()
}
},
destroyed() {
this.editor.destroy();
}
}
</script>

5.編寫測試使用的Vue界面:
<template>
<div>
<navigation></navigation>
<div style="width: 50%;padding-left: 25%;" >
<!--<el-input type="textarea" v-model="form.desc" placeholder="說點什麼吧"></el-input>-->
<div class="components-container">
<el-button @click="getUEContent()" type="primary" style="padding: 10px;margin: 10px;">獲取內容</el-button>
<div class="editor-container">
<ueditor :defaultMsg=defaultMsg :config=config ref="ue"></ueditor>
</div>
</div>
</div>
</div>
</template>

<script>
//採用局部引用的方式註冊組件
import ueditor from '@/components/Ueditor';
export default {
name: "PublishPage",
data() {
return {
defaultMsg: '說點什麼吧',
config: {
initialFrameWidth: null,
initialFrameHeight: 350
}
}
},
components: {
ueditor
},
methods: {
getUEContent() {
let content = this.$refs.ue.getUEContent();
this.$notify({
title: '獲取成功,可在控制檯查看!',
message: content,
type: 'success'
});
console.log(content)
}
}
}
</script>

<style scoped>

</style>

6.編寫路由

7.運行項目測試

npm run devthis

8.效果展現idea

注:

1.編寫的文本和媒體文件發送到服務端請看下一篇介紹。

2.這是筆者學習記錄的過程,若是有錯誤,敬請指正,不喜勿噴,謝謝。

相關文章
相關標籤/搜索