Vue+DRF搭建博客之前端篇(五)

一、任务

  1. 将新建文章和编辑文章区分开
  2. 为v-md-editor添加图片上传的功能
  3. 添加编辑博客大页面

二、将新建文章和编辑文章区分开

1. 修改路由配置

const routes = [
  ...
  {
    path: '/blog',
    component: Blog,
    children: [
      ...
      {path: 'article_edit', component: BlogEditorPage},
      {path: 'article_edit/:article_id', component: BlogEditorPage},
    ]
  },
]

通过url中/blog/article_edit/后有无article_id参数来区分是编辑已有博客还是新建博客

2.在router-view前包一层keep-alive

<!--Blog.vue-->
<template>
  <div>
    <h1>博客页</h1>
    <keep-alive> 
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

3.在BlogEditorPage activated时初始化各个显示的参数

如果url中有article_id参数则视为修改已有文章,没有则视为新建文章:

activated() {
  console.log("activated");
  this.initEditorData();
},
methods: {
  ...
  initEditorData(){
    // 如果是已有文章进行编辑
    if (this.$route.params.article_id !== undefined){
      this.hasArticle = true;
      this.currentArticleId = this.$route.params.article_id;
      blogRequest.getArticle(this.currentArticleId)
      .then(res=>{
        console.log(res)
        this.formData.title = res['article_title'];
        this.formData.abstract = res['article_abstract']
        this.formData.sortTagIds=[]
        for(let each of res['article_tags']){
          this.formData.sortTagIds.push(each.id)
        }
        this.formData.content = res['article_content']
      })
    }
    else{
      this.hasArticle = false;
      this.clearEditor();
      console.log("null")
    }
    this.initSortTags();
  },
  initSortTags(){
    blogRequest.getSortTags().then(res=>{
      this.existingTags = res;
    })
  },
  clearEditor(){
    this.formData.content = "";
    this.formData.contentHtml = "";
    this.formData.abstract = "";
    this.formData.title = "";
    this.formData.sortTagIds = [];
  },
}

4.设置保存和上传文章

onSubmit(){
  //已有文章,进行编辑并保存
  if (this.hasArticle){
    blogRequest.putArticle(this.currentArticleId,this.formData.title,this.formData.abstract, this.formData.content)
    .then(res=>{
      console.log(res)
      blogRequest.articleChangeTags(res['id'], this.formData.sortTagIds)
          .then(res=>{
            console.log("add tag res:", res);
            alert("文章保存成功");
          })
    })
  }
  //新建文章
  else {
    blogRequest.postArticles(this.formData.title, this.formData.abstract, this.formData.content)
      .then(res => {
        console.log(res)
        blogRequest.articleChangeTags(res['id'], this.formData.sortTagIds)
          .then(res=>{
            console.log("add tag res:", res);
            this.$router.push('/article_edit/'+res['id']+'/');
            alert("文章上传成功");
          })
      });
  }
},

这样就完全区分开了两种情况

效果:
新建博客
在这里插入图片描述
编辑博客
在这里插入图片描述

三、为v-md-editor添加图片上传的功能

后端已写好接口:

url功能
/file/upload上传文件

接受的数据格式

{
    file: 文件
    remark: "附带信息"
}

按照v-md-editor进行参数设置:

...
<v-md-editor
    v-model="formData.content"
    height="800px"
    :disabled-menus="[]"
    @upload-image="handleUploadImage"></v-md-editor>
...
// 图片上传相关
handleUploadImage(event, insertImage, files){
  let forms = new FormData()
  forms.append('file',files[0]);
  forms.append('remark', "none");
  blogRequest.postFiles(forms)
  .then(res=>{
    console.log(res);
    // 此处只做示例
    insertImage({
      url:
          res['file'],
      desc: '请输入描述',
      width: 'auto',
      height: 'auto',
    });
  });
},

实现效果:
在这里插入图片描述

四、添加编辑博客大页面

由于我们的BlogEditorPage只是一个vue组件,因而非常容易地就可以脱离原来的居中显示放到大页面(满屏)中去。

路由设置

const routes = [
  ...
  {path: '/article_edit', component: BlogEditorPage},
  {path: '/article_edit/:article_id', component: BlogEditorPage},
]

为SideBar添加tab

data() {
  return {
    sideBarData: [
      {
        moduleName: "所有博客",
        path: "/blog/articles"
      },
      {
        moduleName: "写博客",
        path: "/blog/article_edit"
      },
      {
        moduleName: "大页面编辑文章", <--添加
        path: "/article_edit"
      }
    ]
  }
},

效果:
新建文章
在这里插入图片描述
编辑文章
在这里插入图片描述
此章结束。

上一步:Vue+DRF搭建博客之后端篇(三)
下一步:未完待续

【资源说明】 基于Django和DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip 基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip 基于Django DRF+Vue3开发的考研信息管理系统源码(前端+后端).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载,沟通交流,互相学习,共同进步!
本季课程把开发知识拆解到项目里,让你在项目情境里学知识。这样的学习方式能让你保持兴趣、充满动力,时刻知道学的东西能用在哪、能怎么用。平时不明白的知识点,放在项目里去理解就恍然大悟了。  一、融汇贯通本视频采用了前后端分离的开发模式,前端使用Vue.js+Element UI实现了Web页面的呈现,后端使用Python 的Django REST Framework框架实现了数据访问的接口,前端通过Axios访问后端接口获得数据。在学习完本章节后,真正理解前后端的各自承担的工作。 二、贴近实战本课程为学生信息管理系统课程:Vue3 + Vite + ElementPlus + Django REST Framework项目实战 本季课程主学生信息管理系统V5.0,内容包含:Django REST framework安装和项目初始化、数据的序列化、ViewSet视图集、DefaultRouter路由类、django-filter实现过滤、rest framework实现查找、rest framework实现分页、npm的使用、使用Vite构建vue3项目、Package.json解析、ElementPlus安装和应用、vue-router实现路由、使用Vuex的store对象、后台管理系统主界面的布局、axios组件的安装和请求、axios请求的模块化、请求拦截器和响应拦截器、使用el-select实现联级下拉、使用cascader实现联级选择、vue表单的验证、实现学生信息的添加、修改和删除、实现文件的上传等等功能 本案例完整的演示了项目实现过程,虽然不复杂,但涉及的内容非常多,特别是前后端交互的时候,有诸多的坑等着你去踩,好在王老师全程代码呈现,带着大家一起填坑,大大提高学习效率的同时,也培养了大家良好的代码习惯,希望大家一起跟着王进老师学习Python开发。三、后续课程预告:Vue和Django REST Framework实现JWT登录认证 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值