const HomeComponent = Vue.component('home', {
template: `
<div>
<p>This is my home route</p>
<router-link :to="{name: 'another-route'}">To Another Route</router-link>
</div>`,
mounted: function() {
console.log("In HomeComponent")
}
});
const AnotherComponent = Vue.component('another', {
template: `
<div>
<p>This is my another-route with a sub-component below that has a timer function</p>
<sub-component ref="mySubComponent"></sub-component>
<router-link :to="{name: 'home'}">Home</router-link>
</div>`,
mounted: function() {
console.log("In AnotherComponent")
},
beforeRouteLeave: function(to, from, next) {
console.log("In beforeRouteLeave of AnotherComponent")
// Indicate to the SubComponent that we are leaving the route
this.$refs.mySubComponent.prepareToExit();
// Make sure to always call the next function, otherwise the hook will never be resolved
// Ref: https://router.vuejs.org/en/advanced/navigation-guards.html
next();
}
});
const SubComponent = Vue.component('sub-component', {
template: `
<div>
<p>This is my sub-component with a timer</p>
</div>`,
data: function() {
return {
counter: 0,
twoSecondsTimerEvents: null // This will be initialized in mounted hook
}
},
mounted: function() {
console.log("In SubComponent, starting 'twoSecondsTimerEvents'")
this.twoSecondsTimerEvents = setInterval(() => {
this.counter++;
console.log("Timer event, counter: " + this.counter);
}, 2000)
},
methods: {
prepareToExit: function() {
console.log("Preparing to exit sub component, stopping 'twoSecondsTimerEvents'")
clearInterval(this.twoSecondsTimerEvents)
}
}
});
const ROUTES = new VueRouter({
routes: [{
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.