【问题标题】:Laravel vue.js and vuex link body text by id and show in a new componentLaravel vue.js 和 vuex 通过 id 链接正文文本并显示在新组件中
【发布时间】: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 中设置它:(

标签: laravel vue.js vuex


【解决方案1】:

好的,你可以在你的 vuex“current_state”中创建一个新状态,正如你所说,你可以通过将 id 传递给 vuex 来调度一个突变。

 state: {
    posts: [],
    current_state_id : null
},

在你的突变中

 set_state_id (state, data) {
    state.current_state_id = data;
 }

在您的应用 post.vue 上,您可以设置一个计算属性来监视当前状态

computed: {
  currentState() {
    return this.$store.getters["timeline/current_state_id"];
}}

并为计算属性创建一个观察者来显示当前的id/post

watch: {
  currentState: function(val) {
   console.log(val);
},

【讨论】:

  • 好的,我在 Appshowpost 组件中添加了什么?如何显示来自 id 的帖子?以及如何将帖子的 id 链接到 AppPost.vue 组件上的 post.body 链接? ..感谢您的帮助:) ```` {{ post.body }} ````
  • @StuartCompaan 所以你有正确的帖子列表吗?您可以使用 id 找到它。只需使用 const post = this.posts.find(post => post.id === this.currentState);
  • 对不起,我对 vue.js 和 vuex 很陌生,所以我不知道如何在不同的组件中获取 id ...我在哪里放置 const post = posts.find(post => post .id === this.currentState);
  • 没关系,兄弟,我明白了。在您的 ShowAppPost.vue 中,您可以将它放在我之前发布的 currentState 的 watch 函数下。如果 state id 发生了变化,watch 函数将被触发。获得 id 后,您可以使用 vuex 帖子 getter 查找当前帖子 id。
  • 非常感谢......我会玩弄它并让你知道:)
【解决方案2】:

也许这会对你有所帮助。首先我会推荐使用router-link。如果您有兴趣,请阅读路由器链接here。它非常有用且易于使用。但是您必须在我们的 vue-route 上定义 url 并传递参数(见下文)。

1。您可以将 post.body 包装在 router-link 中,如下所示。

//With this approach, you don't need any function in methods
<router-link :to="'/posts/show/' + post.id">
     {{ post.body }}
</router-link>

2. 在您的 AppSowpost.vue 组件中,您可以根据以下 url 参数找到处于 vuex 状态的帖子。

<template>
  <div> {{ thisPost.body }} </div>
</template>

// ****************

computed: {
  ...mapState({ posts: state=>state.posts }),

// Let's get our single post with the help of url parameter passed on our url
thisPost() { return this.posts.find(p => p.id == this.$route.params.id) || {}; }
},

mounted() { this.$store.dispatch("getPosts");}

3。让我们定义我们的 vue 路由。

path: "posts/show/:id",
name: "showpost",
params: true, // Make sure the params is set to true
component: () => import("@/Components/AppShowPost.vue"),

你的 Mutations 应该像这样简单。

mutations: {
    PUSH_POSTS (state, data) {
        state.posts = data;
    }
},

请告诉我进展如何。

【讨论】:

  • 谢谢 ..我会调查这个 :) 我只是发现很难用 laravel 了解 vue 和 vuex,哈哈
  • 一旦你学会了 vuex,它会让你在管理数据方面变得更轻松。如果我的回答有帮助,请投票并将其标记为正确的。我很确定它会起作用。
  • 感谢您的帮助,我收到此错误? ./resources/js/app.js 中的错误模块未找到:错误:无法解析“/home/stuie/www/authlinks/resources/js”中的“@/Components/AppShowPost.vue”@ ./resources/ js/app.js 20:11-49@multi ./resources/js/app.js ./resources/sass/app.scss
  • 我的 app.js 有这个吗? ```` import VueRouter from 'vue-router' Vue.use(VueRouter) const router = new VueRouter({ mode: 'history', path: "posts/show/:id", name: "showpost", params: true, // 确保参数设置为 true component: () => import("@/Components/AppShowPost.vue") }); const app = new Vue({ el: '#app', store, router }); ````
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 2021-11-03
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多