登陆与注册功能都已经实现,如今是时候来开发文章编辑功能了。javascript
这里我们就使用 markdown
做为编辑语言吧,简洁通用。那么咱们就须要找一下 markdown
的编辑器组件了,并且还要支持 vue
噢。css
若羽这里找到的一个是 mavonEditor
,在 github 上有2k+ 的 star。文档也都是中文的,比较友好。html
首先来安装一下编辑器:java
npm install mavon-editor --saveios
而后在 main.js
中引入组件:git
import Vue from 'vue' import App from './App.vue' import router from './router' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import mavonEditor from 'mavon-editor' import 'mavon-editor/dist/css/index.css' Vue.config.productionTip = false Vue.use(ElementUI) Vue.use(mavonEditor) new Vue({ router, render: h => h(App) }).$mount('#app')
接下来新建咱们的编辑组件了,Edit.vue
:github
<template> <div></div> </template> <script> export default { name: "Edit" } </script> <style scoped> </style>
而后为它添加路由对象:npm
{ path: '/edit', name: 'edit', component: () => import('./views/Edit.vue') }
首先一篇文章有哪些要素:element-ui
最基本是须要这两个要素的。
data
中定义这两个要素:
data() { return { model: { title: '', content: '', } } }
在布局上咱们依旧延续以前的简约风,使用 ElementUI
进行布局。但这里咱们不居中了,直接填满全屏就好。
代码:
<template> <div> <el-row> <el-form> <el-form-item label="文章标题"> <el-col :span="6"> <el-input v-model="model.title"></el-input> </el-col> </el-form-item> <el-form-item> <el-col> <mavon-editor v-model="model.content"></mavon-editor> </el-col> </el-form-item> <el-form-item> <el-col> <el-button type="primary" size="small" @click="submit">发表</el-button> </el-col> </el-form-item> </el-form> </el-row> </div> </template> <script> import axios from 'axios' export default { name: "Edit", data() { return { model: { title: '', content: '', } } }, methods: { submit() { axios.post('https://451ece6c-f618-436b-b4a2-517c6b2da400.mock.pstmn.io/publish', this.model) .then(res => { if(res.data.Code === 200) { this.$message.success('发布成功'); } }) } } } </script>
效果以下:
这个页面也还确实了一部分功能,在发布完成后,应该是要跳转到文章列表的页面去查看全部的文章。
由于列表页面尚未作,因此这里暂时先挖个坑放着~
本篇博文使用了第三方组件,也是在演示如何使用第三方组件来为本身提升开发效率,毕竟不可能全部的东西都本身来从0实现,那多累,还不必定能保证完善。部分第三方组件没法知足的功能就能够考虑本身来实现了。