Skip to content

Commit 191fb28

Browse files
committed
ADD:vuex使用案例
1 parent 3c2f67b commit 191fb28

File tree

7 files changed

+92
-20
lines changed

7 files changed

+92
-20
lines changed

@types/global.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
declare const config: {
2+
version: string;
3+
systemName: string;
4+
debug: boolean;
5+
};
6+
7+
declare interface ResponseBody<T> {
8+
data: T;
9+
code: number;
10+
total: number;
11+
}
12+
declare interface ResponseStatus {
13+
loading: boolean;
14+
error: boolean;
15+
}
16+
declare interface Responsed<T> extends ResponseStatus {
17+
res: ResponseBody<T>;
18+
}
19+
declare interface ResponseNotPage<T> extends ResponseStatus {
20+
data: T;
21+
}
22+
declare interface PageParams {
23+
page: number;
24+
pageSize: number;
25+
}
26+
declare interface RequsetParams extends PageParams {
27+
search: string;
28+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"vue-loader": "^15.7.0",
4949
"vue-style-loader": "^4.1.2",
5050
"vue-template-compiler": "^2.6.10",
51+
"vuex-module-decorators": "^1.0.1",
5152
"webpack": "^4.28.4",
5253
"webpack-cli": "^3.3.2",
5354
"webpack-dev-server": "^3.3.1"

src/pages/home/Home.ts

-9
This file was deleted.

src/pages/home/Home.vue

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@
22
<div class="home">
33
<img alt="Vue logo" src="../../assets/logo.png" />
44
<hello-world msg="vue.js" />
5+
{{ $store.state.home.name }}
6+
<br/>
7+
{{ info }}
8+
<button @click="handleClick">点击</button>
59
</div>
610
</template>
711

8-
<script lang="ts" src="./Home.ts"></script>
12+
<script lang="ts">
13+
import { Component, Vue } from 'vue-property-decorator';
14+
import HelloWorld from '@/components/HelloWorld.vue';
15+
import { HomeModule } from '@/store/modules/home';
16+
17+
@Component({
18+
components: {
19+
HelloWorld
20+
}
21+
})
22+
export default class Home extends Vue {
23+
get info() {
24+
return HomeModule.getMessage;
25+
}
26+
27+
private handleClick() {
28+
HomeModule.updateMessage('liwu');
29+
}
30+
}
31+
32+
</script>

src/store.ts

-10
This file was deleted.

src/store/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Vue from 'vue';
2+
import Vuex from 'vuex';
3+
4+
Vue.use(Vuex);
5+
6+
// Declare empty store first, dynamically register all modules later.
7+
export default new Vuex.Store({});

src/store/modules/home.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { VuexModule, Module, Mutation, Action, getModule } from 'vuex-module-decorators';
2+
import store from '@/store';
3+
4+
@Module({ namespaced: true, dynamic: true, name: 'home', store })
5+
6+
export default class Home extends VuexModule {
7+
public name: string = '游客';
8+
public message: string = '';
9+
10+
get getMessage() {
11+
return `当前时间${new Date()}${this.message}`;
12+
}
13+
14+
@Mutation
15+
private SET_NAME(newName: string): void {
16+
this.name = newName;
17+
}
18+
19+
@Mutation
20+
private SET_MESSAGE(message: string) {
21+
this.message = message;
22+
}
23+
24+
@Action
25+
public updateMessage(newName: string): void {
26+
this.SET_NAME(newName);
27+
this.SET_MESSAGE('欢迎您');
28+
}
29+
}
30+
31+
export const HomeModule = getModule(Home);

0 commit comments

Comments
 (0)