Vue3 单页组件里如何触发 action? #726
-
Vue3 单页组件里如何触发 action?总是提示editor.action is not a function🤔 <template>
<VueEditor :editor="editor" />
<button @click="save">save</button>
</template>
<script>
import { defineComponent } from "vue";
import {
Editor,
rootCtx,
defaultValueCtx,
editorViewCtx,
serializerCtx,
} from "@milkdown/core";
import { nord } from "@milkdown/theme-nord";
import { VueEditor, useEditor } from "@milkdown/vue";
import { commonmark } from "@milkdown/preset-commonmark";
import { emoji } from "@milkdown/plugin-emoji";
export default defineComponent({
name: "Milkdown",
components: {
VueEditor,
},
setup: () => {
const editor = useEditor((root) =>
Editor.make()
.config((ctx) => {
ctx.set(rootCtx, root);
ctx.set(defaultValueCtx, "# Milkdown 💖 Vue");
})
.use(nord)
.use(emoji)
.use(commonmark)
);
const getMarkdown = () =>
editor.action((ctx) => {
const editorView = ctx.get(editorViewCtx);
const serializer = ctx.get(serializerCtx);
return serializer(editorView.state.doc);
});
const save = () => {
let str = getMarkdown();
console.log(str);
};
return {
save,
editor,
};
},
});
</script>
<style>
</style> |
Beta Was this translation helpful? Give feedback.
Answered by
Saul-Mirone
Sep 16, 2022
Replies: 1 comment 1 reply
-
Use |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Congb19
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
getInstance
https://github.com/Saul-Mirone/milkdown/blob/main/examples/vue/Milkdown.vue#L46