Navigate to the store from the main.js file

Having trouble accessing my store in a Vue.js app from the main.js file. The issue is that the store object is showing up as undefined when I attempt to use it. Here's a snippet of my code:

main.js

import { store } from './store/store'

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
    next({ path: '/' })
  } else if (to.path === '/' && store.getters.isLoggedIn) {
    next({path: '/dashboard'})
  } else if (store.getters.isLoggedIn && !to.meta.requiresAuth) {
    next({path: '/dashboard'})
  } else {
    next()
    store.commit('CLOSE_USER_DROPDOWN')
  }
})

store/store.js

import Vue from 'vue'
import Vuex from 'vuex'
import auth from './modules/auth'
import optionDropdown from './modules/option_dropdown'
import userDropdown from './modules/user_dropdown'
import flash from './modules/flash'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export const store = new Vuex.Store({
  plugins: [createPersistedState({ paths: ['auth'] })],
  state: {
  },
  getters: {
  },
  mutations: {
  },
  modules: {
    auth,
    flash,
    userDropdown,
    optionDropdown
  }
})

Despite importing the store, it still shows up as undefined when trying to read from it. Can't figure out why this is happening. Any insights or suggestions would be greatly appreciated.

Answer №1

It seems like the issue lies with the const in the class.

Within the file main.js :

import store from './store/store'

Consider updating your store.js like this: `

export default new Vuex.Store({
plugins: [createPersistedState({ paths: ['auth'] })],
state: {
},
getters: {
},
mutations: {
},
modules: {
auth,
flash,
userDropdown,
optionDropdown
}
})

`

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

What is the best javascript framework to pair with twitter bootstrap?

After dabbling in twitter bootstrap, I discovered its impressive array of UI components. Interested in incorporating it into a project, my quest for a compatible javascript framework has left me torn between angularjs, backbonejs, and emberjs. Although I ...

I have a requirement to save shopping cart information to a MySQL database utilizing the Laravel framework

Looking for guidance on how to insert cart details into a database using Laravel? Here is an example of the request data: cart: [ 0: { product: { product_id: 1, product_name: "Item One" }, quantity: 4 }, 1: { product: { prod ...

transform pixel coordinates to latitude and longitude dimensions

Seeking clarification on how the geo referencing process functions for images. Is there a method to accurately extract latitude and longitude information from this specific line of code? imageBounds = [map.unproject([0, 0], 20), map.unproject([1716,1178], ...

Tips for arranging an array of Date.prototype.toLocaleString() in JavaScript

I am dealing with an array of dates that are in the format Date.prototype.toLocaleString('en-GB'). How can I effectively sort these string-based dates? For example, the input looks like this: ["22/07/2020, 8:54:09 AM", "23/07/2020 ...

Optimizing the performance of "document.createElement"

When attempting to display multiple rows of data in a popup using a for loop, I initially utilized text strings to create and append the div elements. However, I discovered that using document.createElement resulted in a 20% improvement in performance. D ...

Prevent Cross-Site Scripting attacks by filtering the JSON response in a jQuery ajax request

I am facing a peculiar problem where the server is returning JSON responses with XSS-safe text added. There are only two types of responses from the server: An HTML page with a hidden input field containing the desired value A JSON string with the value ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...

The children prop in React Typescript is specified in the props type, but for some reason it is not being

I am currently developing a component library using a combination of React, TypeScript, Styled Components, and Rollup. One of the components I have created is a Button component, defined using a type interface. After rolling up the library and importing th ...

Testing the MatDialog Component

Currently, I am in the process of creating a unit test for my confirmation modal that relies on MatDialog. The initial test I have set up is a simple one to ensure that the component is successfully created. Below is the code snippet from my spec file: im ...

List the attributes that have different values

One of the functions I currently have incorporates lodash to compare two objects and determine if they are identical. private checkForChanges(): boolean { if (_.isEqual(this.definitionDetails, this.originalDetails) === true) { return false; ...

"Encountering a problem with global variables in Nightwatch while running a for loop in Node.js

My current project involves using node, nightwatch, and selenium for automation tasks. I recently encountered a specific scenario that I need assistance with: Within my nightwatch setup, I have declared a global array as follows: Dev.js 'checkforLi ...

Resetting the quiz by utilizing the reset button

Hello everyone, I'm new to this platform called Stack Overflow. I need some help with my quiz reset button. It doesn't seem to be working as intended. According to my code, when the reset button is clicked at the end of the quiz, it should bring ...

Transforming Toggle down div into a sliding animation from right to left using javascript

Hey there, I have two divs - one named basic and the other advanced. There's a button to show the advanced div. Currently, when I click on the button, it toggles down. However, I would like it to slide from right to left instead. Here's the HTML ...

Renaming properties in an AngularJS model

After receiving the data in a structured format, my task is to present it on a graph using radio buttons. Each radio button should display the corresponding category name, but I actually need each button to show a custom label instead of the original categ ...

Add a backslash before certain characters using regular expressions

My current code is quite messy as I have to manually write separate "replace" functions for each special character. var str = ":''>"; str.replace("'","\\'").replace(">","\\>"); I am looking for a way to dy ...

Which is the better choice for simply invoking a service method - subscribe or toPromise?

When implementing the search method below, I simply assign the value of BehaviourSubject in the service. However, I am unsure whether it is possible to execute this operation without using either subscribe() or toPromise() after the .pipe() block in the ...

Error: Incorrect Path for Dynamic Import

Recently, I've been trying to dynamically load locale files based on the locale code provided by Next.js. Unfortunately, every time I attempt a dynamic import, an error surfaces and it seems like the import path is incorrect: Unable to load translatio ...

Encountered an issue while loading a pretrained JSON model in a JavaScript script within a locally hosted

At the moment, I am using TensorFlow in Python to train a model and saving it as model.json along with the BIN file in a folder named models. My goal is to develop a web application that can load this pre-trained model for prediction. However, I have been ...

Issues with navigation drawer not functioning

function adjustMenu() { var navigation = document.getElementById("myTopnav"); if (navigation.className === "topnav") { navigation.className += " responsive"; } else { navigation.className = "topnav"; } } body {margin:0;} ul ...

What is the best way to load a script synchronously from a different directory using an AJAX request?

There are times when I need to load additional JavaScript files through ajax. Initially, I used the standard function provided by jQuery for script loading: $.getScript('script_name.js', callback_function()); However, this approach didn't ...