Skip to content

how to use vuex in vue? #300

Answered by joelgenaro
yuriyleve asked this question in Q&A
Discussion options

You must be logged in to vote

hi here is my solution:

import { createStore } from 'vuex';

const store = createStore({
state() {
return {
count: 0,
};
},
mutations: {
increment(state) {
state.count++;
},
},
actions: {
increment(context) {
context.commit('increment');
},
},
});

export default store;

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

const app = createApp(App);
app.use(store);
app.mount('#app');

Count: {{ count }}

Increment
<script> import { defineComponent } from 'vue'; import { useStore } from 'vuex'; export default defineComponent({ setup() { const store = useStore(); return { count: store.state.count, increment() { store.dispatch('increment'); }, }; }, }); </…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yuriyleve
Comment options

Answer selected by yuriyleve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants