npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-router-metaguards

v0.0.3

Published

A vue utility to extand vue-router navigation guards

Downloads

7

Readme

vue-router-metaguards

Vue official router offers the possibility to hook into the route navigation process under different ways: globally, per-route, or in-component.

The goal of this module is to provide the following extra per-route hooks: beforeEnter, beforeLeave, beforeUpdate, afterEnter, afterLeave, afterUpdate and repeatIn.

Installation

npm install --save vue-router-metaguards

Initialization

import { resolveBeforeGuards, resolveAfterGuards } from 'vue-router-metaguards'
import Router from 'vue-router'

// initialize router
const router = new Router({/* ... */})

// initialize 'before' meta-guards
router.beforeEach(resolveBeforeGuards)

// initialize 'after' meta-guards
router.afterEach(resolveAfterGuards)

Getting started

A hook has to be defined in the meta property of a route. In its simplest use, it consists of a function with two arguments : the source and destination routes.


const router = new Router({
  routes: [{
    path: 'my-route',
    meta: {
      // Will be executed before entering new route
      beforeEnter: (to, from) => { /* ... */ },

      // Will be executed before updating a resolved route
      beforeUpdate: (to, from) => { /* ... */ },

      // Will be executed before leaving a resolved route
      beforeLeave: (to, from) => { /* ... */ },

      // Will be executed after entering a new route
      afterEnter: (to, from) => { /* ... */ },

      // Will be executed after updating a resolved route
      afterUpdate: (to, from) => { /* ... */ },

      // Will be executed after leaving a resolved route
      afterLeave: (to, from) => { /* ... */ },

      // Will be executed every 5s as long as route is resolved
      repeatIn: (to, from) => { /* ... */ },
    }
  }]
})

Notes

  • If you want before hooks to be properly chained, they have to return a promise.
  • The repeatIn hook will be called every 5s as long as the route is enabled.
  • If the repeatIn hook returns a promise, it will wait the promise to be resolved before initiating the 5s delay before the next call
  • The beforeUpdate and afterUpdate hooks are called only for the intersection of the matched source routes and the matched destination routes and only if a route parameter has changed

Advanced usage

If you have many callbacks to run for a single hook you can declare them in an array:


const router = new Router({
  routes: [{
    path: '/app',
    meta: {
      beforeEnter: [
        (to, from) => { /* ... */ },
        (to, from) => { /* ... */ },
        (to, from) => { /* ... */ }
      ]                
    }
  }]
})

If you want more control of your repeatIn hook, you can define it in an object:


const router = new Router({
  routes: [{
    path: '/app',
    meta: {
      repeatIn: {
        delay: 10000,
        trigger: (to, from) => !to.hash,
        handler: (to, from) => { /* ... */ }
      }
    }
  }]
})

In this example:

  • delay is the time in ms between two hooks call
  • trigger is an optional attribute. It is a function returning a boolean. It will be called each time the route is matched and will start or stop the repetition according to the result. Here, trigger is used to enable repetition only if destination route has no hash.
  • handler is the actual function to be repeated

You can also define many repeatIn hooks:


const router = new Router({
  routes: [{
    path: '/app',
    meta: {
      repeatIn: [{
        delay: 10000,
        trigger: (to, from) => !to.hash,
        handler: (to, from) => { /* ... */ }
      }, {
        delay: 3000,
        handler: (to, from) => { /* ... */ }
      }, {
        delay: 6000000,
        trigger: (to, from) => to.hash !== from.hash,
        handler: (to, from) => { /* ... */ }
      }]
    }
  }]
})

API

Full API can be found here

License

vue-router-metaguards is MIT licensed