feat: 实现前后端数据交互功能
- 添加axios API服务模块,封装所有后端接口调用 - 修改各视图组件,移除模拟数据,改为从后端API获取真实数据 - 更新文章、留言板等页面,支持后端数据渲染 - 添加README_API.md文档,说明API调用方式 - 升级axios依赖版本至1.12.2
This commit is contained in:
@@ -5,11 +5,11 @@
|
||||
</div>
|
||||
<div class="post_content">
|
||||
<div v-for="(items, index) in datas" style=" padding: 20px;">
|
||||
<h2>{{ items[index].title }}</h2>
|
||||
<h2>{{ items[index] }}</h2>
|
||||
<span class="badge badge-primary">共{{ contentsum(items) }}篇</span>
|
||||
<ul class="pcont_ul">
|
||||
<li class="pcont_li" v-for="item in items">
|
||||
<a class="btn" @click="btnonclick(item.contentid)"><kbd>{{ item.content }}</kbd></a> — —({{ item.sum }})
|
||||
<a class="btn" @click="btnonclick(item.typeid)"><kbd>{{ item.content }}</kbd></a> — —({{ item.sum }})
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -18,12 +18,26 @@
|
||||
</template>
|
||||
<script setup>
|
||||
import { useRouter } from 'vue-router'
|
||||
import { articleAPI } from '@/axios/api'
|
||||
import { ref, onMounted } from 'vue'
|
||||
const router = useRouter()
|
||||
const btnonclick = (contentid) => {
|
||||
const datas = ref([])
|
||||
console.log("获取文章列表")
|
||||
onMounted(() => {
|
||||
articleAPI.getAllArticles().then(res => {
|
||||
datas.value = res.data
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
}).finally(() => {
|
||||
console.log("finally")
|
||||
})
|
||||
})
|
||||
|
||||
const btnonclick = (typeid) => {
|
||||
router.push({
|
||||
path: '/:type',
|
||||
query: {
|
||||
type: contentid
|
||||
type: typeid
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -34,59 +48,6 @@ const contentsum = (items) => {
|
||||
}
|
||||
return nums
|
||||
}
|
||||
const datas = [
|
||||
// 学习模块
|
||||
[
|
||||
{
|
||||
title: '学习',
|
||||
contentid: '1',
|
||||
content: '算法',
|
||||
sum: 1,
|
||||
|
||||
},
|
||||
{
|
||||
title: '学习',
|
||||
contentid: '2',
|
||||
content: '前端',
|
||||
sum: 1,
|
||||
},
|
||||
{
|
||||
title: '学习',
|
||||
contentid: '3',
|
||||
content: '后端',
|
||||
sum: 1,
|
||||
},
|
||||
{
|
||||
title: '学习',
|
||||
contentid: '4',
|
||||
content: 'Java',
|
||||
sum: 1,
|
||||
},
|
||||
],
|
||||
// 生活模块
|
||||
[
|
||||
{
|
||||
title: '生活',
|
||||
contentid: '1',
|
||||
content: '生活',
|
||||
sum: 1,
|
||||
|
||||
},
|
||||
{
|
||||
title: '生活',
|
||||
contentid: '2',
|
||||
content: '书',
|
||||
sum: 1,
|
||||
},
|
||||
{
|
||||
title: '生活',
|
||||
contentid: '2',
|
||||
content: '书',
|
||||
sum: 1,
|
||||
}
|
||||
]
|
||||
|
||||
]
|
||||
</script>
|
||||
<style>
|
||||
.header {
|
||||
|
||||
@@ -1,15 +1,31 @@
|
||||
<template>
|
||||
<div id="allstyle">
|
||||
<div class="header">
|
||||
<h1>文章内容页面</h1>
|
||||
<h1>{{ article.title }}</h1>
|
||||
</div>
|
||||
<div class="article-content">
|
||||
<p>{{ article.content }}</p>
|
||||
</div>
|
||||
<p>通过传来的{{ urls }}向服务器发送请求获取相关文章的链接</p>
|
||||
</div>
|
||||
|
||||
</template>
|
||||
<script setup>
|
||||
import { useRoute } from 'vue-router';
|
||||
import { articleAPI } from '@/axios/api'
|
||||
import { ref, onMounted } from 'vue'
|
||||
const article = ref({})
|
||||
const urls = useRoute().query
|
||||
// 从后端获取文章详情
|
||||
onMounted(() => {
|
||||
articleAPI.getArticleById(urls.articleid).then(res => {
|
||||
article.value = res.data
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
}).finally(() => {
|
||||
console.log("finally")
|
||||
})
|
||||
})
|
||||
|
||||
</script>
|
||||
<style>
|
||||
</style>
|
||||
@@ -5,7 +5,7 @@
|
||||
class="article-card"
|
||||
v-for="item in datas"
|
||||
:key="item.title + item.publishedAt"
|
||||
@click="aericleClick(item.aur)"
|
||||
@click="aericleClick(item.articleid)"
|
||||
>
|
||||
<h2>{{ item.title }}</h2>
|
||||
<el-text class="mx-1">{{ item.author }}</el-text>
|
||||
@@ -18,8 +18,21 @@
|
||||
|
||||
<script setup>
|
||||
import { useRouter } from 'vue-router'
|
||||
import { articleAPI } from '@/axios/api'
|
||||
import { ref, onMounted } from 'vue'
|
||||
const router = useRouter()
|
||||
|
||||
const datas = ref([])
|
||||
console.log("获取文章列表")
|
||||
onMounted(() => {
|
||||
articleAPI.getAllArticles().then(res => {
|
||||
datas.value = res.data
|
||||
console.log(res.data)
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
}).finally(() => {
|
||||
console.log("finally")
|
||||
})
|
||||
})
|
||||
// 跳转到文章详情
|
||||
const aericleClick = (aur) => {
|
||||
router.push({
|
||||
@@ -27,104 +40,6 @@ const aericleClick = (aur) => {
|
||||
query: { url: aur }
|
||||
})
|
||||
}
|
||||
|
||||
// 文章数据
|
||||
const datas = [
|
||||
{
|
||||
title: '测试1',
|
||||
author: '这是文章模块测试',
|
||||
aur: '链接',
|
||||
mg: '11',
|
||||
publishedAt: '2016-04-10'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
{
|
||||
title: '测试2',
|
||||
author: '这是img模块测试',
|
||||
aur: '链接',
|
||||
mg: '',
|
||||
mg_b: '',
|
||||
publishedAt: '2016-04-12'
|
||||
},
|
||||
]
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<!-- 评论内容 -->
|
||||
<div class="message-item-top">
|
||||
<div class="message-nickname">{{ msg.nickname }}</div>
|
||||
<div class="message-time">{{ msg.time }}</div>
|
||||
<div class="message-time">{{ msg.createdAt }}</div>
|
||||
</div>
|
||||
<div class="message-content">{{ msg.content }}</div>
|
||||
<!-- 回复按钮 -->
|
||||
@@ -86,39 +86,21 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { reactive, ref } from 'vue'
|
||||
import { reactive, ref, onMounted } from 'vue'
|
||||
import { messageAPI } from '@/axios/api'
|
||||
const message_all = ref({})
|
||||
const hoverId = ref(null)
|
||||
const messages = ref([
|
||||
{
|
||||
id: 1,
|
||||
nickname: 'A',
|
||||
content: '这里是A!',
|
||||
time: '2025-09-26 10:00',
|
||||
replies: [
|
||||
{
|
||||
id: 4,
|
||||
nickname: 'B',
|
||||
content: 'A,我来回复你!',
|
||||
time: '2025-09-26 10:20'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
nickname: 'B',
|
||||
content: '这里是B!',
|
||||
time: '2025-09-26 10:05',
|
||||
replies: []
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
nickname: 'C',
|
||||
content: '这里是C!',
|
||||
time: '2025-09-26 10:10',
|
||||
replies: []
|
||||
}
|
||||
])
|
||||
const messages = ref([])
|
||||
// 从后端获取留言列表
|
||||
onMounted(() => {
|
||||
messageAPI.getAllMessages().then(res => {
|
||||
messages.value = res.data
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
}).finally(() => {
|
||||
console.log("finally")
|
||||
})
|
||||
})
|
||||
|
||||
const form = reactive({
|
||||
replyid: null,
|
||||
|
||||
Reference in New Issue
Block a user