link137 link138 link139 link140 link141 link142 link143 link144 link145 link146 link147 link148 link149 link150 link151 link152 link153 link154 link155 link156 link157 link158 link159 link160 link161 link162 link163 link164 link165 link166 link167 link168 link169 link170 link171 link172 link173 link174 link175 link176 link177 link178 link179 link180 link181 link182 link183 link184 link185 link186 link187 link188 link189 link190 link191 link192 link193 link194 link195 link196 link197 link198 link199 link200 link201 link202 link203 link204 link205 link206 link207 link208 link209 link210 link211 link212 link213 link214 link215 link216 link217 link218 link219 link220 link221 link222 link223 link224 link225 link226 link227 link228 link229 link230 link231 link232 link233 link234 link235 link236 link237 link238 link239 link240 link241 link242 link243 link244 link245 link246 link247 link248 link249 link250 link251 link252 link253 link254 link255 link256 link257 link258 link259 link260 link261 link262 link263 link264 link265 link266 link267 link268 link269 link270 link271 link272 link273

[Vue.js] Splitting up Vuex store into separate files gives commit false Subscribe to RSS

when using Vuex in Vue.js.

First, if I put my store inside the main.js. It worked.

But when I split my store into a file called store.js it is not working.

src/store.js

src/main.js

store.js

import vue.js from ‘Vue’
import Vuex from ‘Vuex’

Vue.use(Vuex);

export const store = new Vuex.Store({
state: {
title: ‘Hello from the Vuex Store’,
}
});

main.js

import { store } from ‘./store.js’;

new Vue({
el: ‘#app’,
store: store,
)};

I tried to console.log(store)

and I got a commit false, that is why I know it is not working.
Anything else is correct so I do not include in the codes.

Here is a screenshot of what I console.log:
enter image description here

Solution :

Try it this way instead it has worked for me in the past.

store.js

import vue.js from ‘Vue’
import Vuex from ‘Vuex’

Vue.use(Vuex);

window.store = new Vuex.Store({
state: {
title: ‘Hello from the Vuex Store’,
}
});

main.js

require(‘store.js’);

let vm = new Vue({
el: ‘#app’,
store,
)};

Solution 2:

Somehow magically, I needed to add:

import Vuex from ‘Vuex’
Vue.use(Vuex);

in the main.js for Vuex to work. Anyway, I hope this question helps someone else who is a beginner of Vuex.