Vue+Element使用富文本編輯器的示例代碼
富文本編輯器在任何項(xiàng)目中都會(huì)用到,在Element中我們推薦vue-quill-editor組件,現(xiàn)在我就把它提供給大家,希望對(duì)大家有用。具體截圖如下:

安裝編輯器組件
具體方法:npm install vue-quill-editor --save
編寫(xiě)組件
首先我們?cè)赾omponents文件夾里創(chuàng)建ue.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加載完成后,放入內(nèi)容。
});
},
methods: {
getUEContent() { // 獲取內(nèi)容方法
return this.editor.getContent()
}
},
destroyed() {
this.editor.destroy();
}
}
</script>
在頁(yè)面中使用
下面是使用代碼
<template>
<div>
<el-row class="warp">
<el-col :span="24" class="warp-breadcrum">
<el-breadcrumb separator=">">
<el-breadcrumb-item :to="{path:'/home'}"><b>首頁(yè)</b></el-breadcrumb-item>
<el-breadcrumb-item :to="{path: '/aboutus/aboutlist'}">關(guān)于我們</el-breadcrumb-item>
<el-breadcrumb-item>添加關(guān)于我們</el-breadcrumb-item>
</el-breadcrumb>
</el-col>
<!--
Form 組件提供了表單驗(yàn)證的功能,只需要通過(guò) rule 屬性傳入約定的驗(yàn)證規(guī)則,并 Form-Item 的 prop 屬性設(shè)置為需校驗(yàn)的字段名即可。具體可以參考官網(wǎng):http://element.eleme.io/#/zh-CN/component/form
-->
<el-col :span="24" class="warp-main">
<el-form ref="infoForm" :model="infoForm" :rules="rules" label-width="120px">
<el-form-item label="標(biāo)題" prop="a_title">
<el-input v-model="infoForm.a_title"></el-input>
</el-form-item>
<el-form-item label="來(lái)源" prop="a_source">
<el-input v-model="infoForm.a_source"></el-input>
</el-form-item>
<!--使用編輯器
-->
<el-form-item label="詳細(xì)">
<div class="edit_container">
<quill-editor v-model="infoForm.a_content"
ref="myQuillEditor"
class="editer"
:options="editorOption" @ready="onEditorReady($event)">
</quill-editor>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">確認(rèn)提交</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor' //調(diào)用編輯器
export default {
data() {
return {
infoForm: {
a_title: '',
a_source: '',
a_content:'',
editorOption: {}
},
//表單驗(yàn)證
rules: {
a_title: [
{required: true, message: '請(qǐng)輸入標(biāo)題', trigger: 'blur'}
],
a_content: [
{required: true, message: '請(qǐng)輸入詳細(xì)內(nèi)容', trigger: 'blur'}
]
},
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill
}
},
mounted() {
//初始化
},
methods: {
onEditorReady(editor) {
},
onSubmit() {
//提交
//this.$refs.infoForm.validate,這是表單驗(yàn)證
this.$refs.infoForm.validate((valid) => {
if(valid) {
this.$post('m/add/about/us',this.infoForm).then(res => {
if(res.errCode == 200) {
this.$message({
message: res.errMsg,
type: 'success'
});
this.$router.push('/aboutus/aboutlist');
} else {
this.$message({
message: res.errMsg,
type:'error'
});
}
});
}
});
}
},
components: {
//使用編輯器
quillEditor
}
}
</script>
以上就是全部代碼,謝謝大家,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+Openlayer中使用select選擇要素的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹Vue+Openlayer中使用select選擇要素,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
Vue的指令中實(shí)現(xiàn)傳遞更多參數(shù)
如何在Vue中使localStorage具有響應(yīng)式(思想實(shí)驗(yàn))
Vue3 響應(yīng)式高階用法之triggerRef()的使用
vuejs選中當(dāng)前樣式active的實(shí)例
vue實(shí)現(xiàn)ToDoList簡(jiǎn)單實(shí)例

