安装
npm install wangEditor -s
页面引入
import wangEditor from "wangeditor";
完整代码
<template>
<div class="box">
<div id="editor"></div>
<div class="btn">
<el-button type="primary" @click="getEditorData"
>生成的html代码</el-button
>
</div>
<h3>内容预览</h3>
<div class="preview" v-html="editorData"></div>
</div>
</template>
<script>
import wangEditor from "wangeditor";
export default {
data() {
return {
editor: null,
editorData: "",
};
},
mounted() {
this.initEditor();
},
beforeDestroy() {
// 调用销毁 API 对当前编辑器实例进行销毁
this.editor.destroy();
this.editor = null;
},
methods: {
// 初始化插件
initEditor() {
const editor = new wangEditor("#editor");
editor.config.placeholder = "请输入内容";
editor.config.onchangeTimeout = 0;
editor.config.uploadImgShowBase64 = true;
// 配置 onchange 回调函数,将数据同步到 vue 中
editor.config.onchange = (res) => {
this.editorData = res;
};
// 创建编辑器
editor.create();
this.editor = editor;
},
// 获取编辑器内容
getEditorData() {
let data = this.editor.txt.html();
this.$notify({
title: "html代码",
message: data,
position: "bottom-left",
duration: 0,
});
},
},
};
</script>
<style lang="less" scoped>
.box {
width: 80%;
margin: 20px auto;
}
.btn {
display: flex;
flex-direction: row-reverse;
}
.preview {
width: 100%;
min-height: 300px;
margin: 20px auto 0;
border: 1px solid #ccc;
}
.el-notification.left {
word-break: break-all;
}
</style>
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有