Skip to content

observe router lifecycle hooks #38

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

Closed
tonypee opened this issue Nov 23, 2016 · 6 comments
Closed

observe router lifecycle hooks #38

tonypee opened this issue Nov 23, 2016 · 6 comments

Comments

@tonypee
Copy link

tonypee commented Nov 23, 2016

It would be nice to observe beforeRouteEnter and beforeRouteLeave as lifecycle hooks. Should they be added to the internal hooks list?

They seem as common, if not more common than some of the other hooks, especially for confirming page exit

@ktsn
Copy link
Member

ktsn commented Nov 24, 2016

IMHO, vue-class-component should only depend on the vue core. If it depends on other plugins, the APIs can be wasteful for the users that does not use the plugins.

FYI, you can achieve this by creating a custom decorator.

import { createDecorator } from 'vue-class-component'

const Hook = createDecorator((options, key) => {
  options[key] = options.methods[key]
  delete options.methods[key]
})

// ---------

@Component
class MyComp extends Vue {
  @Hook
  beforeRouteEnter () {
    // ...
  }
}

@fallenleavesguy
Copy link

fallenleavesguy commented Dec 12, 2016

I have copied your code into a third module,vue-router-decorator.
You can use it like this:

import {RouterHook, Router} from 'vue-router-decorator';
@Component({
    name: 'XXComponent'
})
export default class XX extends Vue implements Router{
    @RouterHook
    beforeRouteEnter(to, from, next) {
        next();
    }
    @RouterHook
    beforeRouteLeave(to, from, next) {
        next();
    }
}

@tonypee
Copy link
Author

tonypee commented Dec 13, 2016

I agree with @ktsn that the library should not be bound to to other libs. But, maybe this then calls for the ability to extend it - the same way that Vue.use(VueRouter) adds the hooks to the lifecycle of a component,

Component.use(VueRouter) could extend it with the hooks?

that way, the component is gaining the same extensions as the vanilla vue object

@ktsn
Copy link
Member

ktsn commented Dec 17, 2016

@tonypee Released the extending hooks feature as v4.4.0 :)
https://github.com/vuejs/vue-class-component#adding-custom-hooks

@tonypee
Copy link
Author

tonypee commented Dec 17, 2016

that looks perfect! great work

@renetik
Copy link

renetik commented May 13, 2019

Hook is not called when Component is extending class and its defined in extended class, this is feature ?

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

No branches or pull requests

4 participants