Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS] Clearer usage of prop functions with named views #2493

Closed
tesla3327 opened this issue Nov 22, 2018 · 8 comments
Closed

[DOCS] Clearer usage of prop functions with named views #2493

tesla3327 opened this issue Nov 22, 2018 · 8 comments

Comments

@tesla3327
Copy link

What problem does this feature solve?

I spent a few hours this morning trying to get prop functions with named views to work.

At first I thought that props itself was supposed to be a function that returned an object for all named views:

const routes = [
  {
    path: '/organizations/:id/insights/:widget',
    components: {
      default: SeeMore,
      filter: Filter,
      actionBar: ActionBarBack,
    },
    props: route => ({
      actionBar: {  
        name: route.params.widget,
      },
      filter: {
        hideAutocomplete: true,
      }
    }),
  },
]

After reading and stepping through the vue-router code, I eventually figured out the correct implementation is:

const routes = [
  {
    path: '/organizations/:id/insights/:widget',
    components: {
      default: SeeMore,
      filter: Filter,
      actionBar: ActionBarBack,
    },
    props: {
      actionBar: route => ({  
        name: route.params.widget,
      }),
      filter: {
        hideAutocomplete: true,
      }
    },
  },
]

The docs are not very clear (at least to me) that this is the correct implementation. Not sure if others have had this problem, or if it's just me 🤷‍♂️

What does the proposed API look like?

My suggestion is to add a section to Passing Props to Route Components that explains how to use this with Named Views, along with an example.

Could also add a link to this section in the Named Views section of the docs.

@posva
Copy link
Member

posva commented Nov 22, 2018

This is indeed missing in docs. It should go into https://router.vuejs.org/guide/essentials/passing-props.html if anyone wants to make a contribution
And, as you said, a little note to it in the Named Views section would also be nice.

@suprabhasupi
Copy link

@posva I would like to pick this up

@posva
Copy link
Member

posva commented Nov 26, 2018

go ahead

@suprabhasupi
Copy link

suprabhasupi commented Nov 26, 2018

go ahead

Thanks @posva!
I have created a PR for the this issue.
I have tried to be as simple and direct as possible(As this is my first contribution I might be missing some information to add), Please have a look and let me know if there is any changes required. 🙂

@y-nk
Copy link

y-nk commented Nov 13, 2019

I wished that this PR was merged sooner. Hopefully this ticket was easy to find.

@mikeher
Copy link

mikeher commented Apr 14, 2020

I also just spent an extraordinary amount of time trying to find a solution until I stumbled upon this ancient issue. Thank you for your efforts, this solved my problem.

@douglasg14b
Copy link

douglasg14b commented Jul 7, 2020

I just spent a large amount of time trying to figure this out.... The docs really did not provide the necessary information here, this should be explicitly exampled in it's own section on named views.

Thank you for this.

@douglasg14b
Copy link

douglasg14b commented Jul 7, 2020

Another patterns that is not immediately obvious is:

{
  path: '/settings',
  component: UserSettings,
  props: (route) => resolveProps(route),
  children: [{
    path: 'profile',
    props: { // Important bit here
        default: (route) => resolveProps(route),
        helper: (route) => resolveProps(route)
    }.
    components: {
      default: UserProfile,
      helper: UserProfilePreview
    }
  }]
}

If the childs props is a simple function (ie props: (route) => resolveProps(route)) it silently fails, and is never called. Which can lead to a lot of unnecessary confusion.

@posva posva closed this as completed in 75a18dc Aug 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants