Подключаем api
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
class Misc {
|
class Misc {
|
||||||
async getConfig() {
|
async loadConfig() {
|
||||||
const response = await axios.post('/api/config', {params: ['name', 'version']});
|
const response = await axios.post('/api/config1', {params: ['name', 'version']});
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<el-container>
|
<el-container>
|
||||||
<el-aside :width="asideWidth">
|
<el-aside :width="asideWidth">
|
||||||
|
{{ appName }}
|
||||||
<el-button class="el-button-collapse" @click="toggleCollapse" :icon="buttonCollapseIcon"></el-button>
|
<el-button class="el-button-collapse" @click="toggleCollapse" :icon="buttonCollapseIcon"></el-button>
|
||||||
<el-menu default-active="1" class="el-menu-vertical" @select="handleSelect" :collapse="isCollapse">
|
<el-menu default-active="1" class="el-menu-vertical" @select="handleSelect" :collapse="isCollapse">
|
||||||
<el-menu-item index="1">
|
<el-menu-item index="1">
|
||||||
@@ -27,7 +28,7 @@
|
|||||||
</el-aside>
|
</el-aside>
|
||||||
|
|
||||||
<el-main>
|
<el-main>
|
||||||
Main
|
<pre>{{ apiError }}</pre>
|
||||||
</el-main>
|
</el-main>
|
||||||
</el-container>
|
</el-container>
|
||||||
</template>
|
</template>
|
||||||
@@ -44,7 +45,23 @@ export default @Component({
|
|||||||
class App extends Vue {
|
class App extends Vue {
|
||||||
created() {
|
created() {
|
||||||
this.commit = this.$store.commit;
|
this.commit = this.$store.commit;
|
||||||
this.uistate = this.$store.state.uistate;
|
this.dispatch = this.$store.dispatch;
|
||||||
|
this.state = this.$store.state;
|
||||||
|
this.uistate = this.$store.state.uistate;
|
||||||
|
this.config = this.$store.state.config;
|
||||||
|
}
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
this.dispatch('config/loadConfig');
|
||||||
|
this.$watch('apiError', function(newError, oldError) {
|
||||||
|
if (newError) {
|
||||||
|
this.$notify.error({
|
||||||
|
title: 'Error',
|
||||||
|
dangerouslyUseHTMLString: true,
|
||||||
|
message: newError.response.config.url + '<br>' + newError.response.statusText
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSelect(key, keyPath) {
|
handleSelect(key, keyPath) {
|
||||||
@@ -74,6 +91,14 @@ class App extends Vue {
|
|||||||
return 'el-icon-d-arrow-left';
|
return 'el-icon-d-arrow-left';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get appName() {
|
||||||
|
return `${this.config.name} v${this.config.version}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
get apiError() {
|
||||||
|
return this.state.apiError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,19 @@ import Vue from 'vue';
|
|||||||
import Vuex from 'vuex';
|
import Vuex from 'vuex';
|
||||||
import createPersistedState from 'vuex-persistedstate';
|
import createPersistedState from 'vuex-persistedstate';
|
||||||
|
|
||||||
|
import root from './root.js';
|
||||||
import uistate from './modules/uistate';
|
import uistate from './modules/uistate';
|
||||||
|
import config from './modules/config';
|
||||||
|
|
||||||
Vue.use(Vuex);
|
Vue.use(Vuex);
|
||||||
|
|
||||||
const debug = process.env.NODE_ENV !== 'production';
|
const debug = process.env.NODE_ENV !== 'production';
|
||||||
|
|
||||||
export default new Vuex.Store({
|
export default new Vuex.Store(Object.assign({}, root, {
|
||||||
modules: {
|
modules: {
|
||||||
uistate
|
uistate,
|
||||||
|
config
|
||||||
},
|
},
|
||||||
strict: debug,
|
strict: debug,
|
||||||
plugins: [createPersistedState()]
|
plugins: [createPersistedState()]
|
||||||
});
|
}));
|
||||||
|
|||||||
38
client/store/modules/config.js
Normal file
38
client/store/modules/config.js
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import miscApi from '../../api/misc';
|
||||||
|
// initial state
|
||||||
|
const state = {
|
||||||
|
name: null,
|
||||||
|
version: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
// getters
|
||||||
|
const getters = {};
|
||||||
|
|
||||||
|
// actions
|
||||||
|
const actions = {
|
||||||
|
async loadConfig({ commit, state }) {
|
||||||
|
commit('setApiError', null, { root: true });
|
||||||
|
commit('setConfig', {});
|
||||||
|
try {
|
||||||
|
const config = await miscApi.loadConfig();
|
||||||
|
commit('setConfig', config);
|
||||||
|
} catch (e) {
|
||||||
|
commit('setApiError', e, { root: true });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// mutations
|
||||||
|
const mutations = {
|
||||||
|
setConfig(state, value) {
|
||||||
|
Object.assign(state, value);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
namespaced: true,
|
||||||
|
state,
|
||||||
|
getters,
|
||||||
|
actions,
|
||||||
|
mutations
|
||||||
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
// initial state
|
// initial state
|
||||||
const state = {
|
const state = {
|
||||||
asideBarCollapse: false,
|
asideBarCollapse: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
@@ -11,15 +11,15 @@ const actions = {};
|
|||||||
|
|
||||||
// mutations
|
// mutations
|
||||||
const mutations = {
|
const mutations = {
|
||||||
setAsideBarCollapse(state, value) {
|
setAsideBarCollapse(state, value) {
|
||||||
state.asideBarCollapse = value;
|
state.asideBarCollapse = value;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
state,
|
state,
|
||||||
getters,
|
getters,
|
||||||
actions,
|
actions,
|
||||||
mutations
|
mutations
|
||||||
};
|
};
|
||||||
|
|||||||
25
client/store/root.js
Normal file
25
client/store/root.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// initial state
|
||||||
|
const state = {
|
||||||
|
apiError: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
// getters
|
||||||
|
const getters = {};
|
||||||
|
|
||||||
|
// actions
|
||||||
|
const actions = {};
|
||||||
|
|
||||||
|
// mutations
|
||||||
|
const mutations = {
|
||||||
|
setApiError(state, value) {
|
||||||
|
state.apiError = value;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
namespaced: true,
|
||||||
|
state,
|
||||||
|
getters,
|
||||||
|
actions,
|
||||||
|
mutations
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user