Vue+Element使⽤富⽂本编辑器的⽰例代码富⽂本编辑器在任何项⽬中都会⽤到,在Element中我们推荐vue-quill-editor组件,现在我就把它提供给⼤家,希望对⼤家有⽤。具体截图如下:
安装编辑器组件
具体⽅法:npm install vue-quill-editor --save
编写组件
⾸先我们在components⽂件夹⾥创建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', fig); // 初始化UE
this.editor.addListener("ready", function () {
_this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后,放⼊内容。
});
},
methods: {
getUEContent() { // 获取内容⽅法
return Content()
}
},
destroyed() {
this.editor.destroy();
}
}
</script>
在页⾯中使⽤
下⾯是使⽤代码
<template>
<div>
<el-row class="warp">
<el-col :span="24" class="warp-breadcrum">
<el-breadcrumb separator=">">
<el-breadcrumb-item :to="{path:'/home'}"><b>⾸页</b></el-breadcrumb-item>
<el-breadcrumb-item :to="{path: '/aboutus/aboutlist'}">关于我们</el-breadcrumb-item>
<el-breadcrumb-item>添加关于我们</el-breadcrumb-item>
</el-breadcrumb>
</el-col>
<!--
Form 组件提供了表单验证的功能,只需要通过 rule 属性传⼊约定的验证规则,并 Form-Item 的 prop 属性设置为需校验的字段名即可。具体可以参考官⽹: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="标题" prop="a_title">
<el-input v-model="infoForm.a_title"></el-input>
</el-form-item>
<el-form-item label="来源" prop="a_source">
<el-input v-model="infoForm.a_source"></el-input>
</el-form-item>
<!--使⽤编辑器
-->
<el-form-item label="详细">
<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">确认提交</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor' //调⽤编辑器
export default {
data() {
return {
infoForm: {
a_title: '',
a_source: '',
a_content:'',
editorOption: {}
},
//表单验证
rules: {
a_title: [
{required: true, message: '请输⼊标题', trigger: 'blur'}
asp富文本编辑器],
a_content: [
{required: true, message: '请输⼊详细内容', trigger: 'blur'}
]
},
}
},
computed: {
editor() {
return this.$QuillEditor.quill
}
},
mounted() {
//初始化
},
methods: {
onEditorReady(editor) {
},
onSubmit() {
//提交
//this.$refs.infoForm.validate,这是表单验证
this.$refs.infoForm.validate((valid) => {
if(valid) {
this.$post('m/add/about/us',this.infoForm).then(res => {
Code == 200) {
this.$message({
message: Msg,
type: 'success'
});
this.$router.push('/aboutus/aboutlist');
} else {
this.$message({
message: Msg,
type:'error'
});
}
});
}
});
}
},
components: {
//使⽤编辑器
quillEditor
}
}
</script>
以上就是全部代码,谢谢⼤家,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论