vue3 富文本编辑器wangeditor
npm i @wangeditor/editor@5.1.23 --save
npm i @wangeditor/editor-for-vue@5.1.10 --save
<template>
<div class="editor-wrapper">
<Toolbar
id="toolbar-container"
:editor="editorRef"
:default-config="toolbarConfig"
:mode="mode"
/>
<Editor
id="editor-container"
v-model="modelValue"
:default-config="editorConfig"
:mode="mode"
@on-change="handleChange"
@on-created="handleCreated"
/>
</div>
</template>
<script setup lang="ts">
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { uploadFileApi } from "@/api/file";
const props = defineProps({
modelValue: {
type: [String],
default: "",
},
});
const emit = defineEmits(["update:modelValue"]);
const modelValue = useVModel(props, "modelValue", emit);
const editorRef = shallowRef();
const mode = ref("default");
const toolbarConfig = ref({});
const editorConfig = ref({
placeholder: "请输入内容...",
MENU_CONF: {
uploadImage: {
async customUpload(file: any, insertFn: any) {
uploadFileApi(file).then((response) => {
const url = response.data.url;
insertFn(url);
});
},
},
},
});
const handleCreated = (editor: any) => {
editorRef.value = editor;
};
function handleChange(editor: any) {
modelValue.value = editor.getHtml();
}
onBeforeUnmount(() => {
const editor = editorRef.value;
if (editor == null) return;
editor.destroy();
});
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<template>
<div class="app-container">
<editor v-model="value" style="height: calc(100vh - 180px)" />
</div>
</template>
<script setup lang="ts">
import Editor from "@/components/WangEditor/index.vue";
const value = ref("初始内容");
</script>