Skip to content

Commit 6531f7f

Browse files
committed
'md编辑器上传图片'
1 parent a0c5d89 commit 6531f7f

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/components/common/Sidebar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@
6161
},
6262
{
6363
index: 'vueeditor',
64-
title: '编辑器'
64+
title: '富文本编辑器'
6565
},
6666
{
6767
index: 'markdown',
68-
title: 'markdown'
68+
title: 'markdown编辑器'
6969
},
7070
{
7171
index: 'upload',

src/components/page/Markdown.vue

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
mavonEditor:基于Vue的markdown编辑器。
1212
访问地址:<a href="https://github.com/hinesboy/mavonEditor" target="_blank">mavonEditor</a>
1313
</div>
14-
<mavon-editor v-model="content" codeStyle="tomorrow-night-blue" style="min-height: 600px"/>
15-
14+
<mavon-editor v-model="content" ref="md" @imgAdd="$imgAdd" @change="change" style="min-height: 600px"/>
15+
<el-button class="editor-btn" type="primary" @click="submit">提交</el-button>
1616
</div>
1717
</div>
1818
</template>
@@ -24,12 +24,43 @@
2424
data: function(){
2525
return {
2626
content:'',
27+
html:'',
2728
configs: {
2829
}
2930
}
3031
},
3132
components: {
3233
mavonEditor
34+
},
35+
methods: {
36+
// 将图片上传到服务器,返回地址替换到md中
37+
$imgAdd(pos, $file){
38+
var formdata = new FormData();
39+
formdata.append('file', $file);
40+
// 这里没有服务器供大家尝试,可将下面上传接口替换为你自己的服务器接口
41+
this.$axios({
42+
url: '/common/upload',
43+
method: 'post',
44+
data: formdata,
45+
headers: { 'Content-Type': 'multipart/form-data' },
46+
}).then((url) => {
47+
this.$refs.md.$img2Url(pos, url);
48+
})
49+
},
50+
change(value, render){
51+
// render 为 markdown 解析后的结果
52+
this.html = render;
53+
},
54+
submit(){
55+
console.log(this.content);
56+
console.log(this.html);
57+
this.$message.success('提交成功!');
58+
}
3359
}
3460
}
35-
</script>
61+
</script>
62+
<style scoped>
63+
.editor-btn{
64+
margin-top: 20px;
65+
}
66+
</style>

0 commit comments

Comments
 (0)