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-h-router

v0.0.14

Published

Small router for Vue.js 2 with hash navigation support

Downloads

51

Readme

vue-h-router

npm version npm Build Status

Edge 12+, FF 36+, Chrome 49+, or use translation from ES2105 to ES5

Introduction

vue-h-router is the small router for Vue.js. It deeply integrates with Vue.js core to make building Single Page Applications with Vue.js a breeze. Features include:

  • Lightweight: 2kb of sources
  • No dependencies
  • Modular, component-based router configuration
  • Route params
  • Hash mode navigation
  • HTML5 history support
  • Route hooks

Setup

npm install vue-h-router

Example

import Vue from 'vue';
import router from 'vue-h-router';


Vue.use(router);


const HomeView = {
  template: '<div>Home</div>'
};
const LoginView = {
  template: '<div>Login</div>'
};
const user = {
  authorized: true,
  logout() {
    return fetch('backend/logout');
  }
};
const checkAuth = () => {
  return user.authorized ? Promise.resolve() : Promise.reject();
};
const checkNotAuth = () => {
  return !user.authorized ? Promise.resolve() : Promise.reject();
};

router.init([
  {
    url: '/home',
    component: HomeView,
    before: checkAuth
  },
  {
    url: '/login',
    component: LoginView,
    before: checkNotAuth
  },
  {
    url: '/logout',
    redirect() {
      return user.logout()
         .then(() => {
           user.authorized = false;
           return '/login';
         });
    }
  },
  {
    url: '.*',
    redirect() {
       return Promise.resolve(user.authorized ? '/home' : '/login')
    }
  }
]);

const app = new Vue({
  template: `
    <div class="main">
      <component :is="$route.component"/>
    </div>
  `,
  el: '#app'
});

API

Router Instance

The router instance can be found in inside components as this.$router.

router.init(routes)

Adds a list of routes to router and immediately applies it to the current page url.

import Vue from 'vue';
import router from 'vue-h-router';


Vue.use(router);

const Home = {
  template: '<div>Home content</div>'
};

router.init([
  {
    url: '/home',
    component: Home
  },
  {
    url: /.*/,
    redirect: '/home'
  }
]);

router.route

  • type: Route

The current route represented as a Route Object.

Routes list item fields

url

  • type: String or RegExp

If url is string, then it converts to RegExp:

router.init([
  {
    url: '.*',  // converts to /^.*$/
    component: {}
  }
]);

You can match arguments from url by using parentheses:

router.init([
  {
    url: /^\/department\/(it|cleaning)\/user\/(\d+)$/,
    component: {}
  }
]);

location.hash = '/department/it/user/21';

// router.route.args is ['it', '21'];

component

  • type: Vue Component

redirect

  • type: String

Applies immediately when route matched. Current route is not saved in the history.

location.hash = '/about';

router.init([
  {
    url: '/home',
    component: {}
  },
  {
    url: '/about',
    component: {}
  },
  {
    url: '.*',
    redirect: '/home'
  }
]);

// history is empty
// current url is /about

location.hash = '/not/existed/page';

// history like ['/about']
// current url is /home

redirect(newRoute)

  • return Promise

If promise resolved then redirect applies. If promise rejected then current navigation was aborted.

before(newRoute)

  • return Promise

If promise resolved then redirect applies. If promise rejected then router tries to find next matching route.

after(oldRoute, newRoute)

Called after current route applies.

Router searches for the route in the order in which they appear in the list.

Workflow diagram

Route object

A route object represents the state of the current active route. It contains parsed information of the current URL

The route object is immutable. Every successful navigation will result in a fresh route object.

The route object can be found in multiple places:

  • Inside components as this.$route
  • Inside router as router.route
  • Inside redirect function:
    • redirect(newRoute)
  • Inside navigation hooks:
    • before(newRoute)
    • after(oldRoute, newRoute)

$route.url

  • type: String

The full resolved URL including query.

$route.args

  • type: Array

The list of dynamic segments values.

$route.component

  • type: Vue Component

The Vue component corresponding to the current route.