vue3+vite3+typescript使用wangEditor编辑器
warning:
这篇文章距离上次修改已过191天,其中的内容可能已经有所变动。
在Vue 3 + Vite 3 + TypeScript项目中使用wangEditor编辑器,首先需要安装wangEditor:
npm install wangeditor
然后,在Vue组件中引入并使用wangEditor:
<template>
<div ref="editorRef" style="height: 500px;"></div>
</template>
<script lang="ts">
import { ref, onMounted, defineComponent } from 'vue';
import E from 'wangeditor';
export default defineComponent({
name: 'WangEditorComponent',
setup() {
const editorRef = ref(null);
let editor: E;
onMounted(() => {
editor = new E(editorRef.value as HTMLElement);
editor.create();
// 你可以在这里配置编辑器的更多选项
// 例如:editor.config.x = y;
// 监听内容变化
editor.config.onchange = (newHtml: string) => {
console.log(newHtml);
};
editor.config.onblur = () => {
console.log('编辑器失去焦点');
};
// 创建编辑器
editor.create();
});
return {
editorRef,
};
},
});
</script>
这段代码创建了一个简单的wangEditor实例,并将其挂载到Vue组件的模板中定义的div
元素上。你可以根据需要配置编辑器的更多选项,并监听编辑器中内容的变化。
评论已关闭