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

Allow defining properties in meta #407

Merged
merged 1 commit into from Sep 5, 2020
Merged

Allow defining properties in meta #407

merged 1 commit into from Sep 5, 2020

Conversation

posva
Copy link
Member

@posva posva commented Aug 5, 2020

Ref vuejs/vue-router#3183

Allow extending meta in TS:

declare module 'vue-router' {
  interface RouteMeta {
    requiresAuth?: boolean
    nested: { foo: string }
  }
}

export const router = createRouter({
  routes: [
    // error because `nested` is required
    { path: '/', component: Home, meta: {}
    // No Error because `meta` can be omitted
    { path: '/', component: Home }
  ]
})

I don't know if there is a way of creating an error on the last one without going into generics since it involves making the meta field conditionally required

@github-actions
Copy link

github-actions bot commented Aug 5, 2020

size-limit report

Path Size
size-checks/webRouter.js 6.72 KB (0%)
size-checks/webRouterAndVue.js 23.58 KB (0%)

@posva posva changed the base branch from master to vue2 August 5, 2020 12:27
@posva posva changed the base branch from vue2 to master August 5, 2020 12:27
@posva posva merged commit 706e84f into master Sep 5, 2020
@posva posva deleted the feat/ts-route-meta branch September 5, 2020 10:54
@LiamDotPro
Copy link

LiamDotPro commented Sep 7, 2020

Hey @posva Is there a chance that the router guard next exposed on router.beforeEach((to, from, next) and such will also be fixed with this merge? We originally followed the docs located here: https://router.vuejs.org/guide/advanced/meta.html and added meta:{private:true} to our routes and started passing it using the next callback like below:

next({
          name: 'login',
          path: '/login',
          meta: {
            private: false
          },
          params: { nextUrl: to.fullPath }
})

This approach worked in javascript but now were migrating to typescript I'm unable to figure out how we might be able to satisfy the compiler. I tried extending the routerguard as below but to no alas it still complains.

interface ExtendedNavigationGuard extends NavigationGuardNext<Vue> {
  meta: { private: boolean }
}

I'm not sure it's possible for us to move to a non stable version of the router, so were investigating a workaround. Though If you can verify if this will help out our situation I'd appreciate it! :-)

@LiamDotPro
Copy link

For tracking sakes, here's the failed stackoverflow ticket I opened prior to finding this thread and pull request. I'll update it once It's been resolved https://stackoverflow.com/questions/63754614/how-to-correctly-extend-vue-navigationguardnext-typescript-interface-to-add-meta

@posva
Copy link
Member Author

posva commented Sep 7, 2020

@LiamDotPro meta can only be defined on the route records. That would be the same as passing state, which is not supported, you should definitely replace it with query params or a lib like vuex. You can still modify to.meta to add some kind of during-navigation state but it's still not documented so use it at your own risk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants