效果展示:
首先安装需要的包
npm i @kangc/v-md-editor@next -S
在main.js中进行全局配置
import VMdEditor from '@kangc/v-md-editor/lib/codemirror-editor';
import '@kangc/v-md-editor/lib/style/codemirror-editor.css';
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';
import '@kangc/v-md-editor/lib/theme/style/github.css';
// highlightjs
import hljs from 'highlight.js';
// codemirror 编辑器的相关资源
import Codemirror from 'codemirror';
// mode
import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/css/css';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/mode/vue/vue';
// edit
import 'codemirror/addon/edit/closebrackets';
import 'codemirror/addon/edit/closetag';
import 'codemirror/addon/edit/matchbrackets';
// placeholder
import 'codemirror/addon/display/placeholder';
// active-line
import 'codemirror/addon/selection/active-line';
// scrollbar
import 'codemirror/addon/scroll/simplescrollbars';
import 'codemirror/addon/scroll/simplescrollbars.css';
// style
import 'codemirror/lib/codemirror.css';
VMdEditor.Codemirror = Codemirror;
VMdEditor.use(githubTheme, {
Hljs: hljs,
});
app.use(VMdEditor);
写一个编辑器组件:
<template>
<VMdEditor v-model="text" height="400px"></VMdEditor>
</template>
<script>
import VMdEditor from '@kangc/v-md-editor/lib/codemirror-editor';
export default {
components: {
VMdEditor,
},
data() {
return {
text: '',
};
},
};
</script>
使用组件
<template>
<NavBar></NavBar>
<ContentBase>
<div class="row">
<div class="col-3">
<UserInfo></UserInfo>
</div>
<div class="col-9">
<div class="row-1">
<div class="markdown">
<EditorView></EditorView>
</div>
</div>
<div class="row-1">
<UserPosts></UserPosts>
</div>
</div>
</div>
</ContentBase>
</template>
<script>
import NavBar from "../components/NavBar.vue";
import ContentBase from "../components/ContentBase.vue";
import UserInfo from "../components/UserInfo.vue";
import UserPosts from "../components/UserPosts.vue";
import EditorView from "../components/EditorView.vue";
export default {
name: "UserProfile",
components: {
NavBar,
ContentBase,
UserInfo,
UserPosts,
EditorView,
},
data() {
return {
};
},
methods: {},
};
</script>
<style scoped>
EditorView {
height: 400px;
width: 200px;
}
.markdown {
height: 400px;
/* width: 200px; */
}
</style>
其他的编辑器精美设计请移步官方文档:
https://github.com/imzbf/md-editor-v3/blob/develop/README-CN.md