vue-router-guard-trigger

0.2.0 • Public • Published

Vue Route Guard Trigger

Introduction

Vue Router navigation guards are a very convenient place to define authorization rules. However, they are checked only when the current route changes, but the state of your application can change even when the route doesn't.

This plugin allows you to re-trigger the navigation guards on demand in order to make sure that the user is still authorized to access the current resource.

Getting started

Installation

You can add the plugin as dependency by running the following npm command.

npm install --save vue-route-guard-trigger

You can also use Yarn to accomplish the same thing.

yarn add vue-route-guard-trigger

Importing into the project

The following snippet demonstrates how to import and install the plugin before use.

import VueRouteGuardTrigger from 'vue-route-guard-trigger';

Vue.use(VueRouteGuardTrigger);

API

$triggerCurrentRouteGuards() ⇒ undefined

Trigger all navigation guards defined on the current route.

Example

$triggerCurrentRouteGuards() calls beforeEnter navigation guards on all currently matched routes.

export default {
    name: 'login-popup',
    // ...
    methods: {
        // ...
        onSubmit: function() {
            // ...
            this.afterLogin();
        },
        afterLogin: function() {
            this.$triggerCurrentRouteGuards();
        }
    }
    // ...
};

$triggerGuard(guard, to?) ⇒ undefined

Trigger a specific guard.

Param Type Description
guard function Navigation guard to be triggered
to (this.$route) object Destination route object

Example

You can also trigger a specific guard by passing the name of the guard function to $triggerGuard() method.

function exampleGuard(to, from, next) {
    if (to.param.pass) {
        next();
    } else {
        next(false);
    }
}

this.$triggerGuard(exampleGuard);

License

This project is licensed under Unlicense license. This license does not require you to take the license with you to your project. Read the UNLICENSE file for more information.

Readme

Keywords

none

Package Sidebar

Install

npm i vue-router-guard-trigger

Weekly Downloads

1

Version

0.2.0

License

Unlicense

Unpacked Size

171 kB

Total Files

9

Last publish

Collaborators

  • aleksandar-micic