数学公式
效果
流程
安装插件
bash
pnpm install katex -S
pnpm install @wangeditor/plugin-formula -S
注册插件
注意
引入 import { Boot, IEditorConfig, IToolbarConfig } from "@wangeditor/editor";
引入 import formulaModule from "@wangeditor/plugin-formula";
挂载
onMounted(() => { setTimeout(() => { valueHtml.value = "
模拟 Ajax 异步设置内容
"; }, 1500);// 注册。要在创建编辑器之前注册,且只能注册一次,不可重复注册。 Boot.registerModule(formulaModule); });
代码
vue
<template>
<div style="border: 1px solid #ccc">
<Toolbar
style="border-bottom: 1px solid #ccc"
:editor="editorRef"
:defaultConfig="toolbarConfig"
:mode="mode"
/>
<Editor
style="height: 500px; overflow-y: hidden"
v-model="valueHtml"
:defaultConfig="editorConfig"
:mode="mode"
@onCreated="handleCreated"
/>
</div>
<button @click="getcontent">提交</button>
</template>
<script setup lang="ts">
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from "vue";
import { Boot, IEditorConfig, IToolbarConfig } from "@wangeditor/editor";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import formulaModule from "@wangeditor/plugin-formula";
// 编辑器实例,必须用shallowRef包裹
const editorRef = shallowRef();
// 内容HTML
const valueHtml = ref("<p>hello</p>");
// 编辑器模式
const mode = ref("default"); // simple或者default;
// 模拟ajax异步获取数据内容
onMounted(() => {
setTimeout(() => {
valueHtml.value = "<p>模拟 Ajax 异步设置内容</p>";
}, 1500);
// 注册。要在创建编辑器之前注册,且只能注册一次,不可重复注册。
Boot.registerModule(formulaModule);
});
// 工具栏配置
const toolbarConfig = {
insertKeys: {},
};
toolbarConfig.insertKeys = {
index: 0, // 自定义工具栏中菜单项的顺序,index越小越靠前
keys: ["insertFormula"], // 自定义工具栏中菜单项的key,对应菜单配置中的key属性
};
// 编辑器配置
const editorConfig = {
placeholder: "请输入内容...",
hoverbarKeys: {
formula: {
menuKeys: ["editFormula"],
},
},
};
// 组件销毁的时候,也即时 销毁编辑器
onBeforeUnmount(() => {
const editor = editorRef.value;
if (editor == null) return;
editor.destroy();
});
// 编辑器示例
const handleCreated = (editor: any) => {
editorRef.value = editor; // 记录editor实例
};
// 获取到编辑器内容
const getcontent = () => {
window.alert(valueHtml.value);
};
</script>
<style scoped lang="scss"></style>