index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import * as Vue from 'vue';
  2. import * as Vuex from 'vuex';
  3. import createLogger from 'vuex/logger';
  4. Vue.use(Vuex);
  5. interface ISimpleState {
  6. count: number;
  7. }
  8. const INCREMENT = 'INCREMENT';
  9. const INCREMENT_OBJECT = 'INCREMENT_OBJECT';
  10. function createStore(): Vuex.Store<ISimpleState> {
  11. const state: ISimpleState = {
  12. count: 0
  13. };
  14. const mutations: Vuex.MutationTree<ISimpleState> = {
  15. [INCREMENT] (state: ISimpleState, amount: number) {
  16. state.count = state.count + amount;
  17. },
  18. [INCREMENT_OBJECT] (state: ISimpleState, payload: number) {
  19. state.count = state.count + payload;
  20. }
  21. };
  22. return new Vuex.Store({
  23. state,
  24. mutations,
  25. strict: true
  26. });
  27. }
  28. namespace TestDispatch {
  29. const store = createStore();
  30. store.dispatch(INCREMENT, 1);
  31. store.dispatch({
  32. type: INCREMENT_OBJECT,
  33. silent: true,
  34. payload: 10
  35. });
  36. }
  37. namespace TestWithComponent {
  38. const store = createStore();
  39. const a: vuejs.ComponentOption = {
  40. vuex: {
  41. getters: {
  42. count: (state: ISimpleState) => state.count
  43. },
  44. actions: {
  45. incrementCounter({ dispatch, state }: Vuex.Store<ISimpleState>) {
  46. dispatch(INCREMENT, 1);
  47. }
  48. }
  49. }
  50. };
  51. const app = new Vue({
  52. el: '#app',
  53. components: { a },
  54. store
  55. });
  56. const b: number = app.$store.state.count;
  57. }
  58. namespace TestModules {
  59. interface IModuleAState {
  60. value: number;
  61. }
  62. interface IModuleBState {
  63. value: string;
  64. }
  65. interface IModuleState {
  66. a: IModuleAState;
  67. b: IModuleBState;
  68. }
  69. const aState: IModuleAState = { value: 1 };
  70. const bState: IModuleBState = { value: 'test' };
  71. const aMutations: Vuex.MutationTree<IModuleAState> = {
  72. INCREMENT (state: IModuleAState) {
  73. state.value = state.value + 1;
  74. }
  75. };
  76. const bMutations: Vuex.MutationTree<IModuleBState> = {
  77. APPEND (state: IModuleBState, value: string) {
  78. state.value = state.value + value;
  79. }
  80. };
  81. const a = { state: aState, mutations: aMutations };
  82. const b = { state: bState, mutations: bMutations };
  83. const store = new Vuex.Store<IModuleState>({
  84. modules: { a, b }
  85. });
  86. const valA: number = store.state.a.value;
  87. const valB: string = store.state.b.value;
  88. }
  89. namespace TestPlugin {
  90. const a = (store: Vuex.Store<any>) => {};
  91. const b = (store: Vuex.Store<ISimpleState>) => {};
  92. new Vuex.Store<ISimpleState>({
  93. state: { count: 1 },
  94. plugins: [a, b]
  95. });
  96. }
  97. namespace TestReplaceState {
  98. const store = createStore();
  99. store.replaceState({ count: 10 });
  100. }
  101. namespace TestWatch {
  102. const store = createStore();
  103. store.watch(state => state.count, value => {
  104. const a: number = value;
  105. }, {
  106. deep: true,
  107. immidiate: true
  108. });
  109. }
  110. namespace TestHotUpdate {
  111. const store = createStore();
  112. store.hotUpdate({
  113. mutations: {
  114. INCREMENT (state) {
  115. state.count += 10;
  116. }
  117. }
  118. });
  119. store.hotUpdate({
  120. modules: {
  121. a: {
  122. state: 1,
  123. mutations: {
  124. INCREMENT (state) {
  125. state.value++;
  126. }
  127. }
  128. },
  129. b: {
  130. state: 'test',
  131. mutations: {
  132. APPEND (state, value) {
  133. state.value += value;
  134. }
  135. }
  136. }
  137. }
  138. });
  139. }
  140. namespace TestEvents {
  141. const store = createStore();
  142. const handler = (mutation: Vuex.MutationObject<any>, state: ISimpleState) => {
  143. state.count += 1;
  144. };
  145. store.on('mutation', handler);
  146. store.once('mutation', handler);
  147. store.off();
  148. store.off('mutation');
  149. store.off('mutation', handler);
  150. store.emit('some-event', 1, 'a', []);
  151. }
  152. namespace TestLogger {
  153. const logger = createLogger<ISimpleState>({
  154. collapsed: false,
  155. transformer: state => state.count,
  156. mutationTransformer: m => m
  157. });
  158. new Vuex.Store<ISimpleState>({
  159. state: { count: 1 },
  160. plugins: [logger]
  161. });
  162. }