Importing Vue and Vuex modules from separate files

Recently, I encountered an uncommon issue. I decided to keep my main.js and store.js files separate in a Vue project. In the store.js file, I imported Vuex, set up a new vuex store, and exported it. However, when importing this file into main.js and setting up its requirements (importing Vuex again and using Vue.use(Vuex)), it did not work as expected. I had to add Vue.use(Vuex) inside the store.js file to prevent the error: Error: [vuex] must call Vue.use(Vuex) before creating a store instance.

In the main.js file:


import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

import App from './App.vue'

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

new Vue({
  el: '#app',
  store: store,
  render: h => h(App)
})

And in the store.js file:


import Vuex from "vuex";

export const store = new Vuex.Store({
  state: {
    counter: 0
  }
});

Despite importing

import {store} from './store/store.js';
after Vue.use(Vuex), the Vuex instance inside the store.js did not execute before Vue.use(Vuex).

I suspect that the problem may be related to webpack, but I cannot confirm at this time.

Answer №1

Revise store.js so that it imports Vue and utilizes Vuex:

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export const store = new Vuex.Store({
  state: {
    counter: 0
  }
});

Subsequently, delete Vue.use(Vuex) from the main.js file:

import Vue from 'vue'

import App from './App.vue' 
import {store} from './store/store.js';

new Vue({
  el: '#app',
  store: store,
  render: h => h(App)
})

Having a use statement in store.js is completely acceptable. In fact, this is how it would have been initially generated using a tool like @vue/cli if you opted for the vuex feature.

I hope this clarifies things!

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 way to retrieve the offsetHeight of a Component Element using Vue.js?

I am currently working on a Vue.js component and successfully inserting it into the DOM. My goal is to find out its rendered height once it's in the DOM, specifically its offsetHeight. However, I seem to be missing something obvious as I can't fi ...

What is the simplest way to incorporate Vue with Typescript, without the need for a complex build setup?

I've been spending the last couple of days experimenting with my basic ASP.NET Core website set up for Typescript 2.9, but unfortunately, I haven't made much progress. My main goal is to keep the front-end simple, with just single Vue apps on eac ...

Obtaining a string value through a promise retrieval

Within the following code snippet, I am executing an HTTP request where I extract a "token" (a string) from the response. My objective is to assign this token value to the variable foo. foo = request.post( { url: 'http://10.211.55 ...

Scroll up event and sticky header using jQuery

I'm working on a function $(document).ready(function () { var lastScroll = 0; $(window).scroll(function(event){ var st = $(this).scrollTop(); if ((lastScroll - st) == 5) { $("header").css("position", "fixed"); ...

Transferring properties from the main layout to a specific component

Recently delving into the world of Vue, I find myself in need of passing props from a layout to a global component. Although my search skills may not be up to par, I have been tinkering with a default layout setup as follows: layouts/default.vue <templ ...

JavaScript generated form fails to submit

I am currently facing an issue with submitting form data to a PHP file when it is generated using JavaScript. I am unsure of how to resolve this problem. The form submission works fine on a test .html file by itself, but not when it is generated by JavaScr ...

In Internet Explorer, the loading time of an Angular 2 webpack application is being delayed by the presence of excessive ".js.map" files

https://i.stack.imgur.com/sY0tJ.pngEvery time I attempt to launch my Angular 2 webpack application on IE11, it noticeably takes longer to load compared to using Chrome. Upon inspecting the Network tab, I noticed that IE is attempting to fetch multiple fi ...

Difficulty with AngularJS pagination and encountering errors when trying to read the property 'slice' of an undefined value

Could someone please help me with this issue I'm facing? Here is the code snippet: (function () { var app = angular.module('store', ['ui.bootstrap']); app.controller('StoreController', function ($scope, $http) ...

Producing numerous results from a single input

How can I ensure that users input their address correctly, including street, number, entrance, floor, and apartment, into a single form field without missing any of the values? Additionally, how can I then extract each value (street, number, entrance, floo ...

Combining a group of JavaScript objects

I am facing a challenge with my collection as I need to perform aggregation using only JavaScript. I have attempted various approaches utilizing the Lodash library but unfortunately, I have not been successful. If you could provide me with some guidance on ...

A role requiring coordinates x, y, and z with significant values

http://jsfiddle.net/eho5g2go/ var offset=10000000; ... camera.position.set(offset,offset,400); mesh.position.set(offset-300,offset,0); camera.lookAt(mesh.position); ... animate(); The "offset" variable is crucial for determining the camera and mesh p ...

Troubleshoot: Unable to utilize mapActions with Vuex modules

Having trouble using mapActions to reference actions in my modules. The Vuex docs say that module actions are not namespaced by default, so they should be accessible like main store actions. Here's how I have things set up: Store import * as ModuleA ...

What is the best way to determine the operational schedule of online stores that have varying business days?

Struggling to automatically calculate the working days for various online stores that operate on different schedules. The challenge lies in some of these stores being open on weekends. It's important to note that JavaScript starts counting days of the ...

What is the best way to showcase my subscription form on a web browser?

I am encountering an issue with my code. My goal is to generate a subscribe form modal. Although I have incorporated the subscribe form in my HTML document, upon running the code in the browser, the subscribe section is not visible. Can anyone offer assist ...

Issues persist with AngularJS integration using Modernizr

Incorporating AngularJS and Modernizr, I aim to identify media queries and trigger a function whenever the window or viewport is resized. My goal is to control element visibility based on whether the user is on a desktop or mobile device - certain elements ...

Choose specific columns from distinct tables and arrange them accordingly

I am facing a challenge with my MySQL database as it contains multiple tables with similar columns, and I want to import this data into a Google spreadsheet. For instance: table1 has id, name, page, and other specific columns. table2 also includes id, n ...

Retrieving multiple raw markdown files from GitHub using React JS

Currently, I am facing a challenge in fetching data from multiple raw .md files stored in different folders within a GitHub repository. Although I have successfully retrieved data from one file, my goal is to access all of them. The situation involves a G ...

Get a PDF file from MongoDB via jade Template Engine

Currently, I am utilizing Node, along with express, Jade, and a MongoDB to query the database and showcase the data on a webpage. Within the database, PDFs are stored and my goal is to enable users to download these files directly from the webpage. While ...

Solutions for resolving the issue of not being able to load images when using merged-images in JavaScript

I have a base64 image here and it has already been converted. data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUTEhQWFhUXGBoaGBgYGBcXGhgXGBgYGhgbHhoZHiggGholHhgYITEhJSkrLi4uHR8zODMtNygtLisBCgoKDg0OGhAQGi0lICUtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0 ...

How can I change an array to a string in JavaScript/jQuery and add a specific

Currently, I am tasked with the challenge of converting an array into objects. Furthermore, I also need to add something before each object is displayed. var arrayList = ["image1.jpg","image2.jpg","image3.jpg"]; The desired outcome should look like this ...