Utilizing Vuex Store in Vue for BeforeRouteEnter Hook to Interrupt Navigation

I'm in the process of configuring vue-router to verify a set of permissions before proceeding with navigation to a new route. These permissions are stored in vuex, and I am looking for a way to avoid passing them as props each time.

Even though I use the next(vm => {}) callback, the navigation proceeds to the next page regardless of the outcome, despite expecting a false response to stop the navigation.

beforeRouteEnter(to, undefined, next) {
  next(vm => {
    const allowedRoles = ['Administrator', 'Contributor'];
    if (vm.$store.state.authentication.userInfo.userRoles.some(value => allowedRoles.includes(value))) {
      return true;
    }
    else {
      return false;
    }
  });
}

What mistake am I making here that is causing it to fail?

Answer №1

Ensure to check the value of vm before calling the next function within the beforeRouteEnter method. To access the store in the component, you can import it as shown below:

import store from '@/store/index.js';  // import the store
beforeRouteEnter(to, undefined, next) {
  const allowedRoles = ['Administrator', 'Contributor'];
  const roles = store.state.authentication.userInfo.userRoles;
  const isAllowed = roles.some(value => allowedRoles.includes(value))
  next(isAllowed);  // passes `true` or `false` to `next`
},

This method allows you to access the store without requiring the component instance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I access assets in .vue files with Vapor?

My goal is to distribute assets on Cloudfront with the correct URLs upon deployment. Since I am not utilizing blade, I am unable to use the asset helper and there are no documented instructions for this situation. ...

Creating JSON data that is completely valid using client-side JavaScript

I've been working on creating a JSON explorer/editor. Successfully managed to parse the initial JSON into the div and customize the formatting. Here's the function I utilize to loop through the initial JSON: _iterate(_tab, raw_json){ var tab ...

Top method for implementing select all checkboxes in a table

Hey there! I'm new to VueJS and I've been working on creating a data table component. So far, I have built two components called ui-datatable and ui-checkbox, which allow me to select all rows in the table. It's functioning perfectly fine, b ...

Implement jQuery to dynamically assign an "Active" class to tab elements based on the current page being loaded

INQUIRIES I have include: How do I apply a class to an element using jQuery, or any other method, for the active tab? Ensure that the dropdown tab appearing is the one containing the active tab, not always the Company one. In essence, I want the ac ...

Understanding the Importance and Benefits of Using the Classnames Utility in React Components

Can you break down for me the purpose of utilizing the Classnames utility in React code? I've reviewed the Classnames documentation, but I'm still struggling to comprehend why it is used in code like this: import classnames from 'classnames ...

JavaScript/jQuery function that fails to return a value

Upon performing the following code: test(); function test(){ $('img').load(function(){ alert(this.width); }) } An alert pops up displaying the correct image width. However, when I execute this: alert(test()); function test(){ $(&apos ...

HTML5 Mouse Canvas

Here's a simple example of what's happening: function handleClick(event) { ... } canvas.addEventListener("click", handleClick, false); function drawRectangle(x, y) { context.fillRect(x, y, 16, 16); }; ...

Having trouble with asynchronous JSON calls in JavaScript when setting async to false

I'm having trouble figuring out what I'm doing wrong in this scenario. The issue is that I can't seem to reassign the variable poster_path with the fetched poster-path from the JSON call. What's puzzling to me is that I even tried setti ...

Error in Vue class-based component: Unable to access property 'message' due to its value being null

I am currently exploring the combination of typescript and Vue for the first time in my project. I am encountering an issue that seems to be related to scope, but I could be mistaken. I referenced a small example from VueJS and adapted it as shown below: ...

Avoiding the issue of multiple submissions in Ajax forms

My website's contact form sometimes experiences a delay in sending submissions. When users, in their impatience, click the submit button multiple times, it results in the form being sent repeatedly to the host. To address this issue, I attempted to ...

Summernote information embedded with HTML elements

I just started using the summernote text editor and I'm trying to figure out how to extract the content from the textarea without all the HTML tags. This is what I have in my code: <textarea class="summernote" id="summernote" ng-model="blog.c ...

Optimize Page Speed by Delaying the Loading of Slideshow Images

My main issue with reducing my pagespeed is the slideshow I have implemented. I currently have 17 rather large images in the slideshow, but for simplicity, I will only show the code for 3 images below. I am trying to find a way to load the first image as a ...

Tips for shrinking the size of electron applications

After installing my electron application on Mac, I noticed that it is surprisingly large at 1.39GB, while the Windows version is only around 70MB. Upon unpacking the dmg file, I discovered a file named app.asar which accounts for a significant portion of t ...

Use jQuery to collapse all of the child elements that are currently expanded under a specific parent element

In my list order, I have multiple levels nested within each other: <ul> <li>level 1 a <ul> <li>level 2 a <ul> <li>level 3 a</li> < ...

``Can you provide guidance on excluding matching values from a dictionary object in a Angular project?

I've developed a function that takes a dictionary object and matches an array as shown below: const dict = { CheckAStatus: "PASS", CheckAHeading: "", CheckADetail: "", CheckBStatus: "FAIL", CheckBHeading: "Heading1", CheckCStatus: "FAIL", ...

How can I incorporate a personalized checkbox into a column within a React material table?

I'm currently working on a React project where I am using a Material Table. I am trying to figure out how to add a checkbox in a table cell, for example, in the Birth year column instead of just having the year displayed. Can anyone provide guidance o ...

The jquery script tag threw an unexpected ILLEGAL token

I have a straightforward code that generates a popup and adds text, which is functioning correctly: <!DOCTYPE html><html><body><script src='./js/jquery.min.js'></script><script>var blade = window.open("", "BLA ...

Audio playback system in Node.js

I'm looking to create a seamless playlist of mp3 files that play one after the other. While it may seem straightforward, I'm facing challenges keeping the decoder and speaker channel open to stream new mp3 data in once a song finishes playing. Be ...

Troubleshooting Query Param Problems in EmberJS Route Navigation

("ember-cli": "2.2.0-beta.6") A search page on my website allows users to look for two different types of records: Users or Organizations. The URL for this search page is /search and I have implemented query parameters to maintain the state and enable ba ...

Exporting JSON data to an Excel file using an array of objects with embedded arrays

I am currently developing a fitness application that allows users to create workout routines and download them as an excel file. The data is structured as an array of objects, with each object representing a workout date and containing details of the exerc ...