【发布时间】:2020-10-17 23:07:41
【问题描述】:
我对 Laravel 和 Vuex 很陌生,我的页面上有一组简单的帖子。
测试 1
测试 2
测试 3
我正在尝试链接 AppPost.vue 组件上的文本,并在同一页面上显示已单击新组件 (AppShowpost.vue) 的帖子。我相信我必须通过 id 获取帖子并更改状态?任何帮助都会很好。谢谢。
当您单击测试 1 时,它将在新组件 (AppShowpost.vue) 上显示“测试 1”
在我的商店 timeline.js 中,我相信我需要通过 id 获取帖子并更改状态?
import axios from 'axios'
export default {
namespaced: true,
state: {
posts: []
},
getters: {
posts (state) {
return state.posts
}
},
mutations: {
PUSH_POSTS (state, data) {
state.posts.push(...data)
}
},
actions: {
async getPosts ({ commit }) {
let response = await axios.get('timeline')
commit('PUSH_POSTS', response.data.data)
}
}
}
我的 AppTimeline.vue 组件
<template>
<div>
<app-post
v-for="post in posts"
:key="post.id"
:post="post"
/>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
computed: {
...mapGetters({
posts: 'timeline/posts'
})
},
methods: {
...mapActions({
getPosts: 'timeline/getPosts'
})
},
mounted () {
this.getPosts()
}
}
</script>
我的 AppPost.vue 组件。我需要链接 post.body 以在我的 AppShowpost.vue 组件中显示帖子。
<template>
<div class="w-full inline-block p-4">
<div class="flex w-full">
<p>
<a href="#" @click.prevent=""> {{ post.body }} </a>
</p>
</div>
</div>
</template>
<script>
export default {
props: {
post: {
required: true,
type: Object
}
}
}
</script>
我的 AppSowpost.vue 组件需要显示被点击的帖子。
<template>
<div>
// Displaypost ?
</div>
</template>
<script>
export default {
// Get post from id ?
}
</script>
【问题讨论】:
-
您需要注意状态变化。如果它发生变化,则获取帖子列表。
-
是的,我只是不知道如何在 vuex 中设置它:(