Skip to content

Commit e6e6007

Browse files
committed
migrate to pinia
1 parent 1a0e610 commit e6e6007

File tree

5 files changed

+1121
-865
lines changed

5 files changed

+1121
-865
lines changed

assets/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import * as components from 'vuetify/components';
44
import * as directives from 'vuetify/directives';
55
import '@mdi/font/css/materialdesignicons.css';
66
import { createApp } from 'vue';
7+
import { createPinia } from 'pinia';
78
import VueAxios from 'vue-axios';
89
import axios from './plugins/axios';
910
import App from './App.vue';
1011
import { registerPlugins } from './plugins';
11-
import store from './store/user';
1212

13+
const pinia = createPinia();
1314
const app = createApp(App);
1415
app.use(VueAxios, axios);
15-
app.use(store);
16+
app.use(pinia);
1617

1718
registerPlugins(app);
1819

assets/layouts/default/DefaultLayout.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</template>
7474

7575
<script>
76-
import { mapGetters } from 'vuex';
76+
import { useUserStore } from '../../store/user';
7777
7878
export default {
7979
data: () => ({
@@ -93,12 +93,14 @@ export default {
9393
],
9494
}),
9595
computed: {
96-
...mapGetters({
97-
user: 'getUser',
98-
}),
96+
user() {
97+
const userStore = useUserStore();
98+
return userStore.getUser;
99+
},
99100
},
100101
async mounted() {
101-
await this.$store.dispatch('fetchUser');
102+
const userStore = useUserStore();
103+
await userStore.fetchUser();
102104
this.overlay = false;
103105
},
104106
methods: {

assets/store/user.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
import { createStore } from 'vuex';
1+
import { defineStore } from 'pinia';
22
import axios from '../plugins/axios';
33

4-
// Create a new store instance.
5-
export default createStore({
6-
state() {
7-
return {
8-
user: null,
9-
};
10-
},
11-
mutations: {
12-
setUser(state, user) {
13-
state.user = user;
14-
},
15-
},
4+
export const useUserStore = defineStore('user', {
5+
state: () => ({
6+
user: null,
7+
}),
168
getters: {
17-
getUser(state) {
18-
return state.user;
19-
},
9+
getUser: (state) => state.user,
2010
},
2111
actions: {
22-
async fetchUser({ commit }) {
12+
async fetchUser() {
2313
const { data } = await axios.get('/api/user');
24-
commit('setUser', data);
14+
this.user = data;
2515
},
2616
},
2717
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@vitejs/plugin-vue": "^4.2.3",
2424
"axios": "^1.4.0",
2525
"core-js": "^3.29.0",
26+
"pinia": "^2.1.7",
2627
"roboto-fontface": "*",
2728
"vue": "^3.2.0",
2829
"vue-axios": "^3.5.2",

0 commit comments

Comments
 (0)