【发布时间】:2020-07-15 03:48:40
【问题描述】:
在我的 Vue 应用程序中,我有一个主页视图,可以循环浏览来自 api 的帖子
http://jsonplaceholder.typicode.com/posts/
使用 Axios 是我的index.js 文件
import axios from 'axios'
export default {
fetchPosts () {
return axios
.get('http://jsonplaceholder.typicode.com/posts/')
.then(response => response.data)
}
}
我的主页视图帖子设置,带有从 json 帖子 ID 生成详细信息视图的路由器链接
<li v-for="post in posts" :key="post.title" class="post-item">
<h1>{{ post.title }}</h1>
<router-link :to="{ name: 'details', params: { id: post.id }}"> {{ post.title }}</router-link>
</li>
我的主页脚本
<script>
export default {
name: 'home',
data () {
return {
loading: false
}
},
computed: {
posts () {
return this.$store.state.posts
}
},
created () {
this.loading = true
this.$store.dispatch('fetchPosts')
.then(posts => {
this.loading = false
})
}
}
</script>
Router 链接根据 json id 正确生成详细信息视图,因此 post 1 生成一个名为 /details/1 的页面,因为我当前的路由器已设置
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: Home,
props: true
},
{
path: '/details/:id',
name: 'details',
component: () => import(/* webpackChunkName: "details" */ './views/Details.vue'),
props: true
}
]
})
export default router
在我的详细信息视图/views/Details.vue我有
<template>
<p> The post id is: {{ $route.params.id }} </p>
</template>
从我的主视图The detail is: 1正确检索json
我如何将帖子标题、正文等从主页视图放入详细信息视图?
如果我需要提供任何其他代码,请告诉我,谢谢。
【问题讨论】:
标签: json vue.js vue-router