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

@owlsdepartment/vue-tree-routes

v2.0.4

Published

Small library to write Vue routes definition in a tree structure.

Downloads

34

Readme

vue-tree-routes

Small library to write Vue routes definition in a tree structure.

To add library to project

npm install --save @owlsdepartment/vue-tree-routes

# or if you use yarn

yarn add @owlsdepartment/vue-tree-routes

Versioning

Vue | Vue Router | Vue Tree Routes -- | -- | -- ^3.0.0 | ^4.0.0 | ^2.0.0 ^2.6.0 | ^3.1.6 | ^1.1.1

If you're looking for README.md for v1:

Motivations

Writing routes definitions for Vue Router, you mostly have to keep it all in flat array, even tho you can have /app/note/list, /app/note/add, /app/note/:id and all behave like a children of route /app/note but at the same time, all are using the same <router-view /> component.

This library was created to reflect this logic structure of a tree in our code and, if we want, in our files.

Usage

Every tree node is instance of class RouteNode, that you can create using createRouteNode:

const node = createRouteNode({ /* data */ }) // Preferred way

You can also do it using class itself:

const node = new RouteNode({ /* data */ }) // also possible

What's the difference?

Function createRouteNode will accept children array, that don't have to be an instance of class RouteNode and will convert them to this class, while RouteNode accepts only instances of RouteNode.

Parameters

createRouteNode accepts an object of type RouteDefinition with fields:

interface RouteDefinition {
    shared?: SharedData;
    routes?: RouteRecord[];
    children?: RouteDefinition[];
}

Parameters - routes

Accepts an array of route definitions. It is exctly the same like RouteRecordRaw in vue-router with a small change for TypeScript users: field meta is typed with Meta interface, so you can override it with your own fields. More on that later.

Parameters - children

An array of child nodes. They can be instances of RouteNode.

Parameters - shared

Object with shared data applied on every route. It has two fields:

interface SharedData {
    basePath?: string;
    meta?: Meta;
}
  • basePath - path prefix that will be applied on every route path field in routes array. Can be with or without starting and ending / (some/path, some/path/, /some/path are all accepted). It will be also merged with parent node basePath.

Example

const parent = createRouteNode({
    shared: { basePath: "parent/path" },
    children: []
})

const child = createRouteNode({
    shared: { basePath: "child/path" }
})

Child basePath will be parent/path/child/path. If parent also has an ancestor, it will also be added to child base path like: ancestor/path/parent/path/child/path.

  • meta - meta data that will be applied on every route meta field in routes array only if no meta is defined.

Example

const parent = createRouteNode({
    shared: { meta: { allow: true } },
    routes: [
        { path: "route/1" },                            // meta will be applied
        { path: "route/2", meta: { allow: false } },    // meta won't be applied
    ]
})

Adding to router

As all trees, this also require a root node. Maybe your routes structure has already a root node, but if not, you can use helper createRoot that accepts an array of nodes, either of type RouteDefinition or RouteNode.

const root = createRoot([
    projects,
    info,
    home
])

Than, to add it to out vue-router we simply use spread operator like this:

const router = new Router({
    routes: [
        ...root
    ]
})

It works because RouteNode has implemeneted Symbol.iterator.

Alternative without using root:

const router = new Router({
    routes: [
        ...projects,
        ...info,
        ...home
    ]
})

Overriding Meta interface

As mentioned earlier, if you are using TypeScript, you can define some project-specific fields, that are expected in meta, by using TypeScript module declaration. This is the same way, as suggested by VueRouter.

declare module 'vue-router' {
	interface RouteMeta {
        // insert your meta specific fields here
		requireAuth?: boolean;
	}
}

By default, Meta is an object with any keys and any values, but it is extended from _Meta interface, thus allowing for declaring custom fields.

Full example

const projects = createRouteNode({
    shared: {
        basePath: "projects", meta: { allowedOnly: "Admin" }
    },
    routes: [
        { name: "add-project", path: "add", component: AddProject },
        { name: "edit-project", path: "edit/:id", component: EditProject },
        { name: "delete-project", path: "delete/:id", component: DeleteProject },
        { name: "all-projects", path: "list", component: ListProjects, meta: {} }
    ]
})

const images = createRouteNode({
    shared: {
        basePath: "images", meta: { allowedOnly: "Admin" }
    },
    routes: [
        { name: "add-image", path: "add", component: AddImage },
        { name: "edit-image", path: "edit/:id", component: EditImage },
        { name: "delete-image", path: "delete/:id", component: DeleteImage },
        { name: "all-images", path: "list", component: ListImages, meta: {} }
    ]
})

const adminHub = createRouteNode({
    shared: {
        basePath: "admin"
    },
    routes: [
        { name: "admin-home", path: "", component: AdminView }
    ],
    children: [projects, images]
})

const home = createRouteNode({
    shared: {
        basePath: ""
    },
    routes: [
        { name: "home", path: "home", component: HomeView }
    ],
    children: [adminHub]
})

const router = new Router({
    // some properties...
    routes: [...home]
})

Project Status

Project is new and maintained. And feedback, questions and issues are appreciated.

License

Library is under MIT License