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

v2.0.1

Published

Automatic router for Vue.js

Downloads

16

Readme

Vue Automatic Router

Inspired with NuxtJS routing this package add the similar system to native VueJS project. That is mean that it does not need to change routing by developer. Instead, routes will be added automatically once you add a new page into views directory.

Installation

npm install --save vue-automatic-router

main.js should be changed as

Vue 2

const init = async() => {
  const module = await import('vue-automatic-router');
  const router = await module.default;
  new Vue({
    router,
    render: h => h(App)
  }).$mount('#app');
};

init();

Vue 3

const init = async() => {
  const module = await import('vue-automatic-router')
  const router = await module.default
  createApp(App)
    .use(router)
    .mount('#app')
}

init()

How to add new pages

Automatic router will read your views directory and create appropriate routes. For example:

--views
----index.vue

will create a next routes

[{
    path: '/',
    name: 'Index',
    component: () => import('@/views/index.vue'),
    meta: {
        layout: 'AppDefaultLayout'
        middlewares: {}
    },
    children: []
}]

To create more difficult structure, see next example:

--views
----index
------index.vue
------user.vue
----profile.vue
[
    {
        path: '/',
        name: 'Index',
        component: () => import('@/views/index.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: []
    },
    {
        path: '/user',
        name: 'User',
        component: () => import('@/views/user.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: []
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/views/profile.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: []
    }
]

If you need dynamic parameter in your view, it needs to start your component with _ For example:

--views
----posts
------index.vue
------_id.vue

will create next structure

[
    {
        path: '/',
        name: 'Posts',
        component: () => import('@/views/posts/index.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: []
    },
    {
        path: '/posts/:id',
        name: 'PostsId',
        component: () => import('@/views/posts/_id.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: []
    }
]

To add children (nested routes) to your should start your route with ^ on the same level as the parent

--views
----index
------index.vue
------^post.vue
[
    {
        path: '/',
        name: 'Index',
        component: () => import('@/views/index.vue'),
        meta: {
            layout: 'default'
            middlewares: {}
        },
        children: [
            name: 'Post',
            path: '/post',
            component: () => import('@/views/^post.vue'),
            meta: {
              layout: 'default',
              middlewares: {}
            }
        ]
    }
]

Note restriction: it is not possible now to add second level children

Layouts and middlewares

Vue automatic router allows you to specify layouts and middlewares inside your component.

To add layouts to the project read this article:

https://itnext.io/vue-tricks-smart-layouts-for-vuejs-5c61a472b69b

After that it is possible to add your layout into vue components

export default {
  name: 'Login',
  layout: 'MyLayout'
}

Vue automatic router is looking for the layout "MyLayout.vue" in '/layouts' directory and add it to the route.

It is possible to add vue router middlewares in a vue component.

Here is middleware example

export default function myMiddleware({ next }) {
  return next();
}
import myMiddleware from '...'

export default {
  name: 'Login',
  middlewares: { myMiddleware },
}

Note: middlewares option must be an JS object

Read more about navigation guards here https://router.vuejs.org/guide/advanced/navigation-guards.html