1.0.28 • Published 11 months ago

vue-router-tools v1.0.28

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

Essentials:

1. init-router

Features:

1.Injected router instances into the global routing guard, where 'this' can be used

2.Redefining the replace and push methods to eliminate asynchronous error reporting by routing guards

3.Delayed call to global routing guards, such as when using pinia in the guard, an error will be reported. This attribute can be used to fix this. If there is an infinite loop when resetting the dynamic route(use addRoute), please turn off this option

// router/index.js

import initRouter from 'vue-router-tools/init-router';
import routes from './routes';
import beforeEach from './before-each';
import afterEach from './after-each';

export default initRouter({ routes, beforeEach, afterEach });
// main.js

import router from './router/index';

new Vue({
  render: (h) => h(App),
  router
}).$mount('#app');
descdefaultexplain
beforeEachnavigation guardsfunction (to,from,next) {...}'this' point to router instances in guard is available when not use arrow functions
afterEachnavigation guardsfunction (to,from,next) {...}ditto
lazyHooksdelay call the route guardfalse
...othersvue-router-example

2. add-route

// router/before-each.js

import addRoute from 'vue-router-tools/add-route';

const parentName = 'admin';
let isRouteAdded = false;

export default async function (to, from, next) {
  if (!isRouteAdded) {
    // replace your own dynamic-routes request here
    const routes = await fetch('/get-menu');
    const { replaceTo } = addRoute({
      to,
      routes,
      router: this, // if not use init-router and use arrow function,then the "this" is "undifined",or replace the router instance here
      parentName
    });
    isRouteAdded = true;
    replaceTo();
  } else {
    next();
  }
}

props:

desctype
routesthe dynamic routes from requestArray
routerthe Router's instanceRouter's instance
parentNamethe parentName of dynamic-routesString
tothe navigation guard's "to"Object
notFoundthe RouteConfig of notFoundObject

return value:

desctype
replaceTofor dynamic route first time navigationFunction
resetRouterreset the dynamic routesFunction

3.bread-crumbs

Auto generate dynamic bread crumbs based on vue router

Note:

The route corresponding to each breadcrumb needs to configure the meta attribute, and configure the display text of the route in it

Usage:

import Vue from 'vue';
import Router from 'vue-router';
import autoCrumbs from 'vue-router-tools/bread-crumbs';

Vue.use(Router);
const router = new Router({...})

router.beforeEach((to,from,next)=>{
    const { matched } = to;
  /**
   * @first       --   When there is a homepage, put it at the first of the breadcrumbs
   * @metaNameKey --   Custom your meta name field, the 'name' is default
   */
  const crumbs = autoCrumbs({ matched, first: {text:'',to:''}, metaNameKey:''});
  // The value obtained here should be saved in store (vuex or pinia).
  console.log(crumbs);
    // ...
  next()
})
1.0.28

11 months ago

1.0.27

11 months ago

1.0.26

11 months ago

1.0.25

11 months ago

1.0.24

11 months ago

1.0.23

11 months ago

1.0.22

11 months ago

1.0.21

11 months ago

1.0.20

11 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago