Skip to content

Commit 2db2506

Browse files
committed
修改store模块导入
1 parent 32461ea commit 2db2506

File tree

8 files changed

+50
-10
lines changed

8 files changed

+50
-10
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"vuex-class": "^0.3.2"
3232
},
3333
"devDependencies": {
34+
"@types/node": "^12.7.4",
3435
"@vue/cli-plugin-babel": "^3.0.1",
3536
"@vue/cli-plugin-typescript": "^3.0.1",
3637
"@vue/cli-service": "^3.0.1",

src/components/app-header/AppHeader.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ import {
4444
} from 'vuex-class';
4545
import { currentUser, authToken } from '@/config';
4646
import { storage } from '@/utils';
47-
import { IViewState } from '@/store/view';
47+
// import { IViewState } from '@/store/view';
4848
4949
@Component({
5050
components: {},
5151
})
5252
export default class AppHeader extends Vue {
53-
@State private viewStore!: IViewState;
53+
// @State private viewStore!: IViewState;
5454
@Action('viewStore/toogleSidebar') private toogleSidebar!: () => void;
5555
private username: string = '';
5656

src/servers/Login.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { $post } from '@/utils';
2+
3+
class LoginService {
4+
// 登录接口
5+
public async login(postData: { name: string, password: string }): Promise<any> {
6+
return await $post('/login', postData);
7+
}
8+
}
9+
10+
export default new LoginService();

src/store/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
3+
// tslint:disable-next-line: no-var-requires
4+
const path = require('path');
5+
36
import { viewStore } from './view';
47
Vue.use(Vuex);
58

6-
import user from './modules/user';
9+
/********************************自动导包 start********************************/
10+
const file = require.context('./modules', true, /\.ts/);
11+
const modules: { [propsName: string]: any } = {};
12+
file.keys().forEach((key: string) => {
13+
const name = path.basename(key, '.ts');
14+
modules[name] = file(key).default || file(key);
15+
});
16+
/********************************自动导包 end********************************/
17+
18+
const debug = process.env.NODE_ENV !== 'production';
719

820
export default new Vuex.Store({
21+
strict: debug,
922
modules: {
1023
viewStore,
11-
user,
24+
...modules,
1225
},
1326
});

src/store/modules/login.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import LoginService from '@/servers/Login';
2+
import { errorCaptured } from '@/utils';
3+
4+
export default {
5+
namespaced: true,
6+
state: {},
7+
mutations: {},
8+
actions: {
9+
// 登录的接口
10+
async loginApi(_: any, postData: { name: string, password: string }) {
11+
return await errorCaptured(LoginService.login)(postData);
12+
},
13+
},
14+
};

src/utils/error-captured.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { axiosResult } from './axios-result';
44
* 错误捕获的方法,使用方式errorCaptured(UserService.getAllUser)(getAllUser函数需要传递的参数)
55
* @param func
66
*/
7-
export const errorCaptured = (func: () => void) => {
7+
export const errorCaptured = (func: (_?: any) => Promise<any>) => {
88
return new Proxy(func, {
99
async apply(target, thisBinding, args) {
1010
try {

src/views/shared/login/Login.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
<script lang="ts">
4646
import { Component, Vue, Provide } from 'vue-property-decorator';
4747
import { authToken, currentUser } from '@/config';
48+
import { Action } from 'vuex-class';
4849
import { storage } from '@/utils';
49-
import axios from 'axios';
5050
5151
@Component({})
5252
export default class Login extends Vue {
53+
@Action('login/loginApi') private loginApi!: (postData: { name: string, password: string }) => Promise<any>;
54+
5355
private loginForm: { [propsName: string]: any } = {
5456
email: '',
5557
password: '',
@@ -58,7 +60,7 @@ export default class Login extends Vue {
5860
(this.$refs[formType] as any).validate(async (valid: boolean) => {
5961
if (valid) {
6062
const postData = { name: this.loginForm.email, password: this.loginForm.password };
61-
const { code, result: { data: { token } }, message } = await axios.post('/login', postData);
63+
const { code, result: { data: { token } }, message } = await this.loginApi(postData);
6264
if (code === 0) {
6365
// 设置本地存储
6466
storage.setItem(authToken, token);

0 commit comments

Comments
 (0)