基于SpringBoot + Vue的个人博客系统07——文章列表和文章详情
简介
由于本人不是专业的前端,所以写出来的界面可能会稍微有些丑陋,甚至有些地方的写法不是很专业,还请大家见谅
主界面
JS 部分
首先是 js 逻辑部分
我们先在@/http/request.js
中定义获取文章信息的方法
// 此处省略 axios 的封装,前面的文章中有
export default {getArticles(page, limit) {return instance.get(urls.articles, {params: {page: page,limit: limit}}).then(res => res.data);}
}
然后,下面是Home.vue
中的 js 部分
<script>
import request from "@/http/request";
export default {name: "Home",data() {return {carousel: {title: "竹林客栈",desc: "分享知识,分享生活,分享感动",urls: ["https://tvax1.sinaimg.cn/mw1024/bfe05ea9ly1fxgu8jys3fj21hc0u0k0j.jpg","https://tvax1.sinaimg.cn/large/bfe05ea9ly1fxgunx09dtj21hc0u0q81.jpg","https://tvax1.sinaimg.cn/large/bfe05ea9ly1fxgv2t92yyj21hc0u0qb9.jpg"]},pageInfo: {}};},created() {request.getArticles(1, 5).then(res => {if (res.code === 0) {this.pageInfo = res.data;} else {this.$notify.error({title: "提示",message: res.msg});}}).catch(err => {console.log(err);this.$notify.error({title: "提示",message: "网络忙,文章获取失败"});});}
};
</script>
说明:
- 在 data 中定义 carousel 用于展示轮播图信息,这里先写成固定的,以后我们可以再改成从后端获取
- data 中的 pageInfo 表示一页文章信息
- 然后就是 created 函数在实例已创建,模板还未渲染的时候执行,用于获取文章信息,如果出现错误就弹出提示信息
HTML部分
下面就是数据在页面上的展示,基本就是一些 CSS 和 HTML ,需要注意的是:
- 分类和标签使用了
router-link
标签包裹,这样可以实现点击文字跳转到对应分类或者标签的效果 - 文章标题使用
router-link
跳转的时候把文章 id 作为路径参数传递过去,这样的话在文章详情页面就可以获取到文章 id ,进而根据文章 id 获取文章信息。 - 其中在标题的渲染的时候使用了 v-html 而不是 v-text,是为了方便后期实现根据关键字搜索功能(因为根据实现根据关键字搜索功能的时候,可能需要将某些关键字标红,需要设置行间css样式)
- 文章列表和个人信息部分采用 Element-UI 的分栏布局,详情可参考:https://element.eleme.cn/#/zh-CN/component/layout
<template><div><el-carousel indicator-position="none" height="400px" arrow="nerver" :interval="5000"><el-carousel-item v-for="item in carousel.urls" :key="item"><div class="item-box"><img :src="item" class="carimg" /><div class="desc-box"><h1>{{ carousel.title }}</h1><p>{{ carousel.desc }}</p></div></div></el-carousel-item></el-carousel><el-row :gutter="20"><el-col :span="14" :offset="2"><el-card v-for="article in pageInfo.records" :key="article.id"><div slot="header"><router-link class="main-text" :to="'/post/' + article.id" v-html="article.title"></router-link><div class="article-info"><el-tag effect="dark" size="mini">原创</el-tag>浏览量:{{article.views}} 分类:<router-linkclass="link secondary-text":to="'/category/'+article.category">{{article.category}}</router-link></div></div><div class="tabloid">{{article.tabloid}}</div><i class="el-icon-user-solid article-icon">{{article.author}}</i><i class="el-icon-date article-icon">{{article.gmtCreate}}</i><i class="el-icon-price-tag article-icon"><router-linkclass="tag"v-for="(tag,index) in article.tags":key="index"v-text="tag":to="'/tag/'+tag"></router-link></i></el-card></el-col><el-col :span="6"><el-card>个人信息</el-card></el-col></el-row></div>
</template><script>
// ......上面已经说过,不再赘述
</script><style scoped>
.item-box {position: relative;width: 100%;height: 100%;
}
.carimg {width: 100%;height: 100%;overflow: hidden;object-fit: cover;
}
.desc-box {position: absolute;bottom: 0;left: 50%;top: 50%;width: 500px;height: 40px;margin-left: -250px;margin-top: -20px;text-align: center;
}
.el-card {margin-top: 20px;
}
.article-info {margin-top: 10px;color: #909399;font-size: 13px;
}
.article-icon,
.article-icon .tag {color: #909399;font-size: 13px;margin-right: 10px;text-decoration: none;
}
.article-icon .tag:hover {color: #409eff;cursor: pointer;
}
.tabloid {color: #606266;font-size: 14px;margin-bottom: 10px;
}
</style>
页面效果
文章详情
1、在 @/http/request.js
中封装根据 id 获取文章信息的方法
getArticleByID(id) {return instance.get(urls.article + "/" + id).then(res => res.data);}
2、新建 Article.vue 用于展示页面详情
<template><el-row><el-col :span="20" :offset="2"><el-card>{{article}}</el-card></el-col></el-row>
</template><script>
import request from "@/http/request";
export default {name: "Article",data() {return {article: {}};},created() {request.getArticleByID(this.$route.params.id).then(res => {if (res.code === 0) {this.article = res.data;} else {this.$notify.error({title: "提示",message: res.msg});}}).catch(err => {console.log(err);this.$notify.error({title: "提示",message: "网络忙,文章详情获取失败"});});}
};
</script><style scoped>
.el-card {margin-top: 15px;padding: 20px;
}
</style>
说明:
- 当组件创建的时候获取文章信息
- 使用
this.$route.params
可以获取路由的路径参数,即文章 id
3、在路由 @/router/index.js
中注册 Article.vue 组件对应的路由
{path: "/post/:id",name: "Article",component: () => import("@/views/Article.vue"),meta: {title: "文章详情",},},
4、将 markdown 转为 html
markdown 转 html 有很多种方法,可以在前端做转换,也可以在后端转换,这里我们采用前端转换,使用Showdown.js
我们可以使用在项目中导入 js 文件的形式转换,也可以使用大佬封装好的 Vue 组件,这里我们使用一个叫做 VueShowdown 的组件,文档:https://vue-showdown.js.org/zh/guide/
首先,安装 VueShowdown 以及它的插件
npm install vue-showdown
npm install showdown-highlight # 代码高亮插件,只是转化,并没有对应 css
npm install highlight # 引入 highlight.js 主要是想使用,代码高亮的 css
在 main.js 中引入相应的 css,不然高亮不生效, highlight.js 中自带很多 css 高亮样式,这里我们使用 github 样式
import 'highlight.js/styles/github.css'
在 main.js 中注册组件
import { VueShowdown } from 'vue-showdown'
Vue.component('VueShowdown', VueShowdown)
使用组件:
<template><el-row><el-col :span="20" :offset="2"><el-card><VueShowdown:markdown="article.content"flavor="vanilla":options="{ emoji: true, tables: true }":extensions="extensions"/></el-card></el-col></el-row>
</template>
说明:
- options 中配置开启 emoji 表情和表格解析
注意此时的data中的 article需要修改为如下格式,不然会报错。
data() {return {article: { content: "" },extensions: [showdownHighlight]};},
此时,我们的markdown可以转换为html,代码也可以高亮了。但是原生的 html 样式有些丑,这个时候,我们就可以使用一个好看的css样式来美化项目,这里我们使用的是github-markdown-css
安装:
npm install github-markdown-css
在main.js中引入,这里这里的 github-markdown.css 和上面引入的 highlight.js/styles/github.css 不同,后者只有高亮效果
import 'github-markdown-css/github-markdown.css';
说明:
- 这里的css名称以及路径都可以在 node_modules 目录中找到,如下图中 CSS 文件的路径为
github-markdown-css/github-markdown.css
所以在main.js
中导入的路径就为github-markdown-css/github-markdown.css
最后,在需要使用该样式的外层加入class="markdown-body"
即可
<template><el-row><el-col :span="20" :offset="2"><el-card class="markdown-body"><h1>{{article.title}}</h1><blockquote>{{'作者:' + article.author + ' | 创建时间:' + article.gmtCreate + ' | 浏览数:' + article.views + ' | 分类:' + article.category + ' | 标签:' + article.tags.join("、")}}</blockquote><VueShowdown:markdown="article.content"flavor="vanilla":options="{ emoji: true, tables: true }":extensions="extensions"/></el-card></el-col><el-backtop></el-backtop></el-row>
</template>
说明:
- 上面的 CSS 文件也可以在
index.html
中直接引入,但是不建议这样用,因为容易出现在某个组件中直接刷新,CSS 文件加载不出来的情况 - el-backtop 为Element-UI 提供的回顶部组件
- 标题和文章信息暂时就用 h1 和 blockquote 来展示了,随后可能会进行美化
效果图:
表格渲染:
参考代码:https://gitee.com/qianyucc/QBlog2/tree/v-5.0
基于SpringBoot + Vue的个人博客系统07——文章列表和文章详情相关推荐
- 基于SpringBoot + Vue的个人博客系统12——使用vue-admin-template展示文章列表(后台管理)
简介 前面我们实现了博客系统的前台展示页面,还有留言功能没有实现,实现留言功能无非就是在后端增加留言表,对留言进行增删改查.和文章表类似,这里就不在赘述. 既然作为一款动态博客,那么后台管理是必不可少 ...
- 推荐一个基于Springboot+Vue的开源博客系统
简介 这是一个基于Springboot2.x,vue2.x的前后端分离的开源博客系统,提供 前端界面+管理界面+后台服务 的整套系统源码.响应式设计,手机.平板.PC,都有良好的视觉效果! 你可以拿它 ...
- 基于SpringBoot + Vue的个人博客系统16——文章的修改和删除
简介 删除文章 删除功能比较简单,只需进行如下操作: 调用删除接口删除文章 然后再刷新文章列表 修改文章 在文章列表页面点击修改文章按钮 跳转到写文章页面,同时带上文章 id 作为参数 在写文章界面创 ...
- 基于springboot + vue 的个人博客搭建过程(上线)
承接上文: 基于springboot + vue 的个人博客搭建过程(续) 目录 1. 搭建环境 1. 安装docker 2. 拉取并运行 2.1 拉取服务 2.2 部署运行mysql 2.3 部署运 ...
- 基于springboot + vue 的个人博客搭建过程(续)
承接上文:基于springboot + vue 的个人博客搭建过程 目录 1. 评论列表 1.1 接口说明 1.2 controller 1.3 service 1.4 mapper 1.5 实体类 ...
- 基于springboot搭建的个人博客系统
源码下载地址:blog blog是基于springboot搭建的个人博客,响应式 前端技术:html.css.js.jq.bootstrap 后台技术:springboot.thymeleaf.myb ...
- 基于SpringBoot + Vue的个人博客
博客介绍 基于Springboot + Vue 开发的前后端分离博客 在线地址 项目链接: www.ttkwsd.top 后台链接: admin.ttkwsd.top Github地址: https: ...
- java基于springboot+vue的旅游博客旅游经验分享系统
如今社会飞快发展,人们生活节奏不断加快,压力也随之变大.为了释放压力,缓解疲劳,大多数人会选择旅游.但是现在基本上很少有免费网站注重介绍张家界的,大部分都是以"商"为主提供导游.酒 ...
- 使用 ThinkJS + Vue.js 开发博客系统
编者注:ThinkJS 作为一款 Node.js 高性能企业级 Web 框架,收到了越来越多的用户的喜爱.今天我们请来了 ThinkJS 用户 @lscho 同学为我们分享他基于 ThinkJS 开发 ...
最新文章
- 微型计算机步进电机控制,步进电机的微型计算机控制
- 移动端框架如雨后春笋,你该如何选择?
- STC自动高速下载线
- Hibernate CRUD操作
- pyharm虚拟环境_手把手教你如何在Pycharm中加载和使用虚拟环境
- OpenCASCADE:OCCT应用框架OCAF之函数机制使用示例
- mysql数据自动备份_每天自动备份MySQL数据库的shell脚本
- 【转】WCF Data Service 使用小结(二) —— 使用WCF Data Service 创建OData服务
- 【报告分享】与AI共进,智胜未来:智能金融联合报告-埃森哲百度.pdf
- 135.001 智能合约设计-——单员工薪酬系统
- golang1.1-基础环境的配置以及事项
- BMVC2020 Best Paper: Delving Deeper into Anti-aliasing in ConvNets论文解读
- 设计模式之多用组合,少用继承
- 【AngularJS】 # AngularJS入门
- xxl-job 带参数执行和高可用部署
- 淘宝模板开发系列之DOM、CSS规范
- 《ERP高级计划》书解读-APS案例分析之五时间点的计算(蔡颖)(转)
- php在文本框显示图片,多张图片上传后在页面上可以显示图片,在文本框显示地址怎么解决啊...
- go文件服务器加密,gosignal: 使用 Golang 实现的端对端加密聊天软件 Signal 服务端...
- 360全景拍摄为什么要使用鱼眼镜头,与超广角镜头区别?