14

Simple task to pass data from one page to another made a real headache to newbie in Vue.js Here is my router enter image description here

I'm rendering a form on "/" page, which make an API request, enter image description here

and I just want to pass data to another route page with another component enter image description here

Is it so hard to do in Vue? It made a real headache for me today. Get data from API, save & send to another component. How can I do it?

Evgeny Malkov
  • 437
  • 1
  • 5
  • 18

4 Answers4

15

As Antony said, since you've already enabled props in router/index.js, you can pass parameters in router.push like this:

router.push({
    name: 'events',
    params: {
        items: data
    }
});

then you will be able to receive items as props in EventsDisplay.vue

export default {
    name: 'eventsDisplay',
    props: ['items'],
};
choasia
  • 10,404
  • 6
  • 40
  • 61
  • 1
    @EzhiMakov `this.items` should work. Since it's not related to this question, you can just open another question and paste your code there. – choasia Aug 04 '17 at 08:59
  • thanks a lot, man, sorry for previous comment, it was my stupdity with refreshing /events page without passing data – Evgeny Malkov Aug 04 '17 at 09:19
  • thanks it did worked.. wasted so much time.. finally got – Ramesh_D Jun 27 '18 at 07:20
  • @choasia, May i ask you to have a look at a Vue.JS question related with the source code in github of the book 'Full-Stack Vue.js 2 and Laravel 5' by Anthone Gore here : https://stackoverflow.com/questions/59245577/laravel-vuejs-router-view-does-not-render-the-component ? Particularly take a look at the EDIT: section of the OP – Istiaque Ahmed Dec 13 '19 at 11:34
  • 2
    In my case (@vue/cli-service version 3.12.1), I cannot get the params through props but I get it from `this.$route.params.items` – juacompe Apr 13 '21 at 07:01
  • please add "props: true", in router prop define like this => { path: "/dataList", name: "dataList", props: true, meta: { title: "Data list" }, component: () => import("path to datalist component") } – Wasi Sadman Dec 05 '22 at 15:48
2

If you want to save it globally, you could use an event bus or a VueX store to contain it, then save and load it from this event bus or store when you need to access it.

If you want to pass it between those two components only, and only when accessing the second page from the first one, you can pass properties to the route argument as per this guide: https://router.vuejs.org/en/essentials/passing-props.html

Antony
  • 1,253
  • 11
  • 19
1

Try using Vuex

In Source Component

this.$store.commit('changeDataState', this.$data);
this.$router.push({name: 'user-post-create'});

In Destination Component

created () {
    console.log('state', this.$store);
    this.$store.getters.Data;
}
0

Add props true in your vue router


{
    path: "/pay-now",
    name: "pay",
    props: true,
    component: () =>
      import(/* webpackChunkName: "about" */ "../components/account/pay.vue"),
    beforeEnter: AuthGuard,
  },

After that pass props with router like that

        this.$router.push({ 
        name: "pay",
        params: {
        result:this.result,
      }
      });

Shamaz saeed
  • 387
  • 3
  • 8