store/index.js
import Vue from 'vue';
import Vuex from 'vuex';
import backend from '../axios/backend';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
latestYear: 2020,
},
mutations: {
latestYearUpdated(state, y) {
state.latestYear = y;
},
},
actions: {
async getLatestYear(store) {
let resp = await backend.get('/datas/years/latest');
if (resp.status === 200) {
let latestYear = resp.data.latestYear;
console.log(resp.data, latestYear);
store.commit('latestYearUpdated', latestYear);
}
},
},
modules: {},
});
hello.vue
import { mapState, mapActions } from 'vuex';
export default {
name: 'Search',
data: () => {
return {
code: '',
};
},
computed: {
...mapState(['latestYear']),
},
methods: {
search() {
let code = this.code;
let year = this.latestYear;
let params = { code };
let query = { year };
this.$router.push({ name: 'Student', params, query });
},
...mapActions(['getLatestYear']),
},
mounted() {
this.getLatestYear();
},
};