36

I have the following fiddle https://jsfiddle.net/91vLms06/1/

const CreateComponent = Vue.component('create', {
  props: ['user', 'otherProp'],
  template: '<div>User data: {{user}}; other prop: {{otherProp}}</div>'
});

const ListComponent = Vue.component('List', {
  template: '<div>Listing</div>'
});

const app = new Vue({
  el: '#app',
  router: new VueRouter(),
  created: function () {
    const self = this;
        // ajax request returning the user
    const userData = {'name': 'username'}

    self.$router.addRoutes([
      { path: '/create', name: 'create', component: CreateComponent, props: { user: userData }},
      { path: '/list', name: 'list', component: ListComponent },
      { path: '*', redirect: '/list'}
    ]);
    self.$router.push({name: 'create'}); // ok result: User data: { "name": "username" }; other prop:
    self.$router.push({name: 'list'}); // ok result: Listing

    // first attempt
    self.$router.push({name: 'create', props: {otherProp: {"a":"b"}}}) // not ok result: User data: { "name": "username" }; other prop:
    self.$router.push({name: 'list'}); // ok result: Listing

    // second attempt
    self.$router.push({name: 'create', params: {otherProp: {"a":"b"}}}) //not ok result: User data: { "name": "username" }; other prop:
  }
});

As you can see first I am passing to CreateComponent the user just when I initialize the route.

Later I need to pass another property otherProp and still keep the user parameter. When I try to do this the object I send is not passed to the component.

How can I pass the otherProp and still keep the user?

The real purpose of the otherProp is to fill a form with the data from it. In the listing part I have the object and when I click the "edit" button I want to populate the form with the data that comes from the listing.

YakovL
  • 7,557
  • 12
  • 62
  • 102
tzortzik
  • 4,993
  • 9
  • 57
  • 88

3 Answers3

41

It can work by using props's Function mode and params

Vue 2 demo: https://jsfiddle.net/hacg6ody/

when adding routes, use props's Function mode so that it has a default property user and it will add route.params as props.

{
    path: '/create',
    name: 'create',
    component: CreateComponent,
    props: (route) => ({
        user: userData,
        ...route.params
    })
}

params passed in push will automatically be added to props.

self.$router.push({
    name: 'create',
    params: {
        otherProp: {
            "a": "b"
        }
    }
})
redfox05
  • 3,354
  • 1
  • 34
  • 39
Jacob Goh
  • 19,800
  • 5
  • 53
  • 73
  • 3
    and how one makes such params to appear in the url, for instance in "query" (pseudoquery, if that's not history mode of the router)? – YakovL Apr 02 '20 at 16:36
  • Lost you on the line that is "...route.params". What are the three dots for? – Craig Aug 23 '20 at 16:50
  • 1
    @Craig that's https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – Jacob Goh Aug 24 '20 at 01:45
  • 2
    Note: The fiddle above no longer works as it seems to be using latest Vue version, and the fiddle is written in Vue 2. Use this one instead to get the Vue 2 one working: https://jsfiddle.net/hacg6ody/ I submitted an edit to the answer to help other readers. Old fiddle was: https://jsfiddle.net/jacobgoh101/mo57f0ny/1/ – redfox05 Jul 27 '22 at 10:15
3

Here's my simple solution to send object using Router.

this.$router.push({
 name: 'someName'
 params: {
  obj: {...someObject}
 },
});

And if you want to use obj in the next page. You Can get data below.

this.$route.params.obj
YOUNG MIN CHO
  • 253
  • 1
  • 3
  • 13
3

I'm using the following setup in Vue Router 4 and Vue 3 and Composition API:

  1. In the router, define your route like:

    {
      name: 'someroute',
      path: 'someroute?id=:id&name=:name`,
      component: [YourComponentNameHere],
      props: true,
    }
    
  2. In the sender component:

    router.push('someroute',
      params: {
        id: 1,
        name: 'John'
      }
    })
    
  3. In the receiver component, access them using:

    router.currentRoute.value.params.id
    router.currentRoute.value.params.name
    
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Using vue3 (with ts) and your awnser worked. But I would like to pass an entire item for a list > edit feature. Did you find something – jplattus May 10 '23 at 14:41
  • 1
    @jplattus: You can try `JSON.stringify()` to convert your item to a string and then pass it through `params` or even `query` option of Vue router. – dotNET May 11 '23 at 04:36