question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

declaration of beforeRouteEnter(to,from,next) hook should be updated

See original GitHub issue

Version

3.0.1

Reproduction link

https://jsfiddle.net/doommm/50wL7mdz/74369/

Steps to reproduce

  • Vue 2.5.3
  • Vue-class-component 6.1.0
  • Typescript 2.6.1
  • vscode 1.17.2, vetur 0.11.0

I found that in declaration file(types/router.d.ts), there’s a type named NavigationGuard used for beforeRouteEnter and other two hooks. The function declaration of next is ((vm: Vue) => any) | void) => void, and should it be updated so we could access all members declared in class(or object)? I wrote three examples to explain my confusion.

  1. In .vue file with object literal style, i cant access my own properties.
  2. In .vue file with vue-class-component, if i add an type identifier manually, it works. no error. if not, error.
  3. In separate .ts file with vue-class-component, whether i add identifier or not, got error. But if i roll back to typescript 2.5.3, and add type identifier, it works.

I think the declaration should be updated(Generics? T extends Vue or ?), but i’m not sure about that. Sorry for my poor english If I dont describe the problem clearly.

What is expected?

all class(object) members should be accessed in vm.propertyName

What is actually happening?

We could only access members of Vue.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:6
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

11reactions
doommmcommented, Nov 9, 2017

Consider this example. Blog is used to be only accessible via List component, so you annotate next as (vm: List) => any. Later, we add Blog links in Author’s page. next typing is wrong then!

I’m still confused. There must be something i misunderstood or even never know.

I’m focusing on the In-Component guards beforeRouteEnter(to,from,next), and they(In-Component guards) should be tightly coupled with the component(or should be reusable?).

So for the example you posted, do you mean to define beforeRouteEnter in Blog? Shouldn’t it become (vm: Blog) => any?

// Component List
@Component<List>({
  beforeRouteEnter(to, from, next) {
    next((vm: List) => {
      vm.blog; // accessible
    });
  },
})
class List extends Vue {
  blog: any;
}
// Component Author
@Component<Author>({
  beforeRouteEnter(to, from, next) {
    next((vm: Author) => {
      vm.blog;// accessible
    });
  },
})
class Author extends Vue {
  blog: any;
}
// Component Blog
@Component<Blog>({
  beforeRouteEnter(to, from, next) {
    next((vm: Blog) => {
      vm.p;// accessible
    });
  },
})
class Blog extends Vue {
  p: any;
}
8reactions
tinobinocommented, Mar 22, 2021

@websmurf, @webrsb Please try this:

import { ComponentPublicInstance, defineComponent, ref } from 'vue'
import { NavigationGuardNext, RouteLocationNormalized } from 'vue-router'

type CustomNameParamProvider = {
  customNameParam: string
} & ComponentPublicInstance

function isCustomNameParamProvider (vm: ComponentPublicInstance): vm is CustomNameParamProvider {
  return (vm as CustomNameParamProvider).customNameParam !== undefined
}

export default defineComponent({
  beforeRouteEnter: (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    next(vm => {
      if (isCustomNameParamProvider(vm) && typeof from.name === 'string') {
        vm.customNameParam = from.name
      }
    })
  },
  setup () {
    const customNameParam = ref('')
    return {
      customNameParam
    }
  }
})

This is using an “user-defined type guard” for narrowing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Additional Hooks
registerHooks allows you to register such hooks: ... hooks beforeRouteEnter(to, from, next) { console.log('beforeRouteEnter') next() } beforeRouteUpdate(to, ...
Read more >
Vue Router with vue-class-component: next function does ...
Please move your beforeRouteEnter hook to @Component decorator. @Component({ template: '<div></div>', beforeRouteEnter(to, from, next){ ...
Read more >
Navigation Guards | Vue Router
Navigation confirmed. Call global afterEach hooks. DOM updates triggered. Call callbacks passed to next in beforeRouteEnter guards with instantiated instances.
Read more >
Vue.js
The way we determine the async-ness of these hooks is convoluted. ... I imagine that after replacing routes router would check if current...
Read more >
How To Do More With Vue Router
After installing Axios, we can update our Home.vue to look like this: ... beforeRouteEnter(to, from, next) { axios ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found