2023-11-13 07:45PM
文章列表页面的代码如下:
src/components/ArticleList.vue
<template>
<div>
<div v-for="article in articles" :key="article.id">
<router-link :to="{ name: 'ArticleShow', query: { id: article.id } }">
<p>
{{ article.created_at }}
{{ article.title }}
</p>
</router-link>
</div>
</div>
</template>
<script>
export default {
data () {
return {
articles: []
}
},
mounted () {
this.$http.get('http://localhost:3000/api/articles')
.then(response => {
console.info(response.data)
this.articles = response.data.articles
})
.catch(error => {
console.error(error)
})
}
}
</script>
文章详情页面的代码如下:
src/components/ArticleShow.vue
<template>
<div>
<p>
{{ article.title }}<br/>
{{ article.created_at }}
</p>
<p>
{{ article.content }}
</p>
</div>
</template>
<script>
export default {
data () {
return {
article: {}
}
},
mounted () {
this.$http.get('http://localhost:3000/api/articles/' + this.$route.query.id)
.then(response => {
this.article = response.data
})
.catch(error => {
console.error(error)
})
}
}
</script>
路由文件的代码如下:
src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import ArticleList from '@/components/ArticleList'
import ArticleShow from '@/components/ArticleShow'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/articles',
name: 'ArticleList',
component: ArticleList
},
{
path: '/article',
name: 'ArticleShow',
component: ArticleShow
}
]
})
然后在浏览器打开文章,就发现已经可以了:
登录
请登录后再发表评论。
评论列表:
目前还没有人发表评论