Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue, is there a way to pass data between routes without URL params?

I am looking how to pass data secretly between two separate components (not parent and child) without using URL params in my Vue2 app. This doesn't mean I am passing secrets but rather I just dont want the user to see it (only for UI considerations).

I know Vue has Props but they are meant for passing data between parent and child component. In my case, my URL will change but I don't want to pass data via visible params.

Someone claimed to use props without URL params here but I haven't been able to reproduce a working solution (getting undefined each time).

I also checked out these options but they are all using either URL or query params which as we know are visible.

An ugly solution would be to write the data to local storage and then read it there but this creates a lot of overhead and complexity (like what if I only want this data to be read once, etc).

Is there a more elegant solution to this problem?

Thanks!

like image 312
KasparTr Avatar asked Jun 23 '18 05:06

KasparTr


People also ask

How do I transfer data from one route to another Vue?

Using Props To Share Data From Parent To Child # VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!

What is params in Vue?

In Vue Router we can use a dynamic segment in the path to achieve that, we call that a param: const User = { template: '<div>User</div>', } // these are passed to `createRouter` const routes = [ // dynamic segments start with a colon { path: '/users/:id', component: User }, ]

How do you use a router to push?

Note: Inside of a Vue instance, you have access to the router instance as $router . You can therefore call this.$router.push . To navigate to a different URL, use router.push . This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.

How do you send props to Vue?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.


2 Answers

  1. make props: true for the destination route -- in the index.js file of router

    {        path: '/home',        name: 'home',        component: taskChooser,        props: true,        }
  1. define prop in the component e.g props: ['myprop'], - note the quotes

  2. copy the variable you want to pass from the source route into the same name as your prop - in this case myprop

myprop = theVariableThatYouWantToPass    this.$router.replace({name:'home', params:{myprop}});

Make sure that the name of prop and variable are same - the prop is in quotes.

It's working for me.

like image 105
mayank1513 Avatar answered Oct 06 '22 12:10

mayank1513


Thanks @Mayank for pointing me in the correct direction.

Here is the correct syntax that worked for me.

  1. Notice the props in In router index

    {   path: '/componentPath',   name: 'componentName',   props: {      header: true,      content: true   }, } 
  2. In the component you are redirecting to, define the props as following:

    props: {   myProperty: {     type: <DATATYPE>   }, } 
  3. Perform redirect as following:

    this.$router.push({   name: 'componentName',   params: {     myProperty: <VARIABLE>   } }) 
  4. Access props with the this. convention from created or in later lifecycle event.

In this case, the variable name and property name do not have to be the same as it is a simple map. That naming convention would be a curious design choice anyway.

like image 39
KasparTr Avatar answered Oct 06 '22 12:10

KasparTr