How to use router component lifecycle hooks?

How can I attach a lifecycle hook into the router component? I tried all possibilities, adding on the on property, adding on the router property.

Hey… what is this difference / advantage using Lifecycle Hooks compared to the typical “on” events?

Well, they have different lifecycle timestamps or not sure how to call it. Like there are no hooks for page:init, animation events. Hooks work with all components, e.g. Popup, Tab’s content, Panel, etc.

Ok - thx… but let’s say you have a component page to perform some initial ajax requests. I can do this like:

...
created(){
    this.getData();
}, 
...

or

...
on: {
    pageInit: function (e, page) {
        this.getData();
    },
},
...

this is quite the same behavior, no? When do lifecycle timestamps matter? Can you give me an example when it is better to use lifecycle hooks instead of usual on events?

In this case there is no much difference. Just keep in mind that if you use some DOM operation in created hook, the component’s element is not yet in DOM

1 Like