Attempt 1
main.js
import { createApp } from 'vue'
import { store } from './store/store'
import App from './App.vue'
Vue.config.productionTip = false
const app = createApp(App)
app.use(store)
app.mount('#app')
store.js
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
patients: []
}
});
Attempt 1 Error
Uncaught TypeError: Cannot read property 'use' of undefined
at eval (store.js?07a4:4)
at Module../src/store/store.js (app.js:1342)
Attempt 2
main.js
import { createApp } from 'vue'
import { store } from './store/store'
import App from './App.vue'
Vue.config.productionTip = false
const app = createApp(App)
app.use(store)
app.mount('#app')
store.js
import Vuex from 'vuex'
import Vue from 'vue'
export const store = new Vuex.Store({
state: {
patients: []
}
});
Attempt 2 Error
vuex.esm.js?94ea:135 Uncaught Error: [vuex] must call Vue.use(Vuex) before creating a store instance.
at assert (vuex.esm.js?94ea:135)
at new Store (vuex.esm.js?94ea:380)
at eval (store.js?07a4:6)
I created a Vue application using Vue CLI. I am trying to implement Vuex. I have followed numerous tutorials (usually set up 2 different ways). When using a debugger, I was able to stop before it read "Vue.use(Vuex)". Logging Vue at this point returns undefined. I believe my imports are correct but do not know if I am missing something crucial. Any help is much appreciated.
EDIT: The only difference between Attempt 1 and 2 is I removed "Vue.use(Vuex)" on Attempt 2.
You're using vuex 3 syntax with Vue 3 which is compatible with vuex 4 that has the following syntax :
store.js
import { createStore } from 'vuex'
// Create a new store instance.
const store = createStore({
state: {
patients: []
}
})
export default store;
then use the exported store in the main.js :
import { createApp } from 'vue'
import store from './store/store'
import App from './App.vue'
Vue.config.productionTip = false
const app = createApp(App)
app.use(store)
app.mount('#app')
Vuex 4 docs
Vue.js 3 docs
I was able to get this working by uninstalling my VueX (3.6)
npm uninstall vuex
and installing the VueX (4.0 alpha)
npm i vuex#4.0.0-alpha.1
The reason for this error was because I was using 2 different syntax methods from different versions.
Thanks for everyone's help!
main.js
import { createApp } from 'vue'
import { store } from './store/store'
import App from './App.vue'
const app = createApp(App)
app.use(store)
app.mount('#app')
store.js
import { createStore } from 'vuex'
// Create a new store instance.
const store = createStore({
state () {
return {
patients: []
}
}
})
export default store;
Related
hello everyone i am using laravel mix to create my vue js app recently. It compiles correctly with no errors. But on the browser console, it says:Vuex is not defined.
Heres is the code. The guide i am using wrote it exactly that way with no mistakes. so i dont really know where the error is coming from. Heres the code:
import Vue from "vuex";
import getters from './getters';
import mutations from './mutations';
import * as actions from './actions';
import Vuex from "vuex";
Vuex.use(Vuex);
export const store = new Vuex.store({
modules:{
},
state:{
},
getters,
mutations,
actions
})
You are mixing what Vue is and what Vuex is.
Try something like the following:
import Vuex from "vuex";
import getters from './getters';
import mutations from './mutations';
import * as actions from './actions';
export const store = new Vuex.createStore({
modules:{
},
state:{
},
getters,
mutations,
actions
});
and wherever you do createApp() with Vue,
import { store } from 'path/to/storefile.js';
...
const app = createApp(...)
app.use(store);
I'm using express with NodeJS for running the APIs
For Authentication I'm using the below package
const session = require('client-sessions');
app.use(session({
cookieName: 'session',
secret: process.env.COOKIE_SECRET,
duration: 30 * 60 * 1000 * 30,
activeDuration: 60 * 60 * 1000 * 30
}));
I'm using express
I have imported axios and VueAxios in my main.js file with the below code:
import { createApp } from 'vue'
import App from './App.vue'
import './assets/style.css'
import router from './router'
import axios from 'axios';
import VueAxios from 'vue-axios';
const app = createApp(App)
app.use(VueAxios, axios, router)
app.provide('axios', axios)
app.provide('VueAxios', VueAxios)
app.mount('#app')
Here is the code in my Component Javascript Block
import { onBeforeMount, onMounted, ref, inject } from "vue";
setup() {
const axios = inject('axios');
const VueAxios = inject('VueAxios');
onMounted(async ()=> {
VueAxios.axios.get(...... // I get this error - Cannot read property 'get' of undefined
VueAxios.get(...... // I get this error - VueAxios.get is not a function
axios.get(...... // this works, but how do I use VueAxios for passing session payload?
})
}
Adding the below line of code after app.provide('VueAxios', VueAxios) made it work
axios.defaults.withCredentials = true;
I have an Vue App.
I use vuex.
I created my app like this:
import { createApp } from "vue";
import axios from "axios";
import App from "./App.vue";
import router from "./router";
import store from "./store/index";
axios.defaults.baseURL = "https://localhost:44349";
const app = createApp(App)
.use(router)
.use(store)
.mount("#app");
Than i one of my component i am trying to access context.root.$store in the setup() method
, but context.root is undefined.
<script>
import {ref, computed } from "vue";
export default {
name: "ClientList",
setup(props, context) {
const clients = ref([]);
console.log(context);
const clientOrdersLenght = computed(() => {
return clients.value.length;
});
return { clients, clientOrdersLenght }
},
};
</script>
My idea is get acces to my store via context.root. I watched videos and examples with this. but they refer to Vue 2 using 'vue/composition-api' as import.
What i am missing?
You might be able to include and access the store directly
store.dispatch('myModule/myModuleAction')
store.myModule.state.blabla
import {ref, computed } from "vue";
import store from './store/index'; // include store
export default {
name: "ClientList",
setup(props) {
const clients = ref([]);
const clientOrdersLength = computed(() => {
store.dispatch('myAction'); // call dispatch
store.state.myItem // or access store's state
return clients.value.length;
});
return { clients, clientOrdersLength }
},
};
I found an easy way to access my store with getStore() from vuex. That's cool.
But for other reason in the feature, me or somebody else will need to access the $root (vue instance) of the app. So may be now the correct question is how to get the $root (vue instance) of the app in a child component?
main.js:
import Vue from "vue";
import Axios from "axios";
import App from "./App.vue";
import router from "./router";
import store from "./store";
const axios = Axios.create({
baseURL: process.env.VUE_APP_BASE_URL
})
Vue.prototype.$http = axios;
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
when I try to use $store in App.vue it is undefined:
created() {
this.$store.dispatch('logout') // this.$store is undefined
}
store.js:
export default new Vuex.Store({
actions: {
logout({commit}) {
return new Promise((resolve, reject) => {
commit('logout')
localStorage.removeItem('token')
delete Vue.prototype.$http.defaults.headers.common['Authorization']
resolve()
})
}
}
}
Is it because it is not initiated in App.vue, because I can use at other vue components.
You haven't imported Vuex in Main.JS:
import Vuex from 'vuex'
Vue.use(Vuex)
Learn more about installing Vuex here
EDIT: Legit everyone just stole my answer...
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
// Options
});
And just put to the Vue Instance.
Hope it helps you.
First, you need to install Vuex using npm install vuex --save
Then, import it into your store.js.
To understand WHEN, WHY and HOW to use Vuex, refer this URL https://dev.to/napoleon039/when-why-and-how-to-use-vuex-9fl
I am having an issue calling up an action from my Vue component using this.$store.dispatch('lookupName'). I have tried console logging the this.$store method, but the actions within it are empty.
Main.js
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import { findCertificate } from '#/api';
Vue.use(Vuex)
const state = {
// single source of data
nameLookup: {},
}
const actions = {
// asynchronous operations
lookupName(context, form){
return findCertificate(form)
}
}
const store = new Vuex.Store({
state,
actions,
mutations,
getters
})
export default store
Vue Component
import VueRecaptcha from 'vue-recaptcha';
import { mapGetters, mapActions} from 'vuex'
export default {
created() {
},
methods: {
...mapActions({
lookupName: 'lookupName'
}),
...mapActions({
add: 'lookupName'
}),
onCaptchaVerified: function(recaptchaToken){
this.$refs.recaptcha.reset();
console.log(this.$store.dispatch('lookupName', this.form))
},
}
}
Why am I still getting this error? I have looked up many other people's questions about similar issues, but those solutions did not work for me.