index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import * as Vue from 'vue';
  2. import * as Vuex from '../index';
  3. import createLogger from '../../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 INumberState {
  60. value: number;
  61. }
  62. interface INestedModuleState extends INumberState {
  63. a: INumberState;
  64. b: INumberState;
  65. }
  66. interface IModuleState {
  67. nested: INestedModuleState;
  68. c: INumberState;
  69. }
  70. const aState = { value: 1 };
  71. const bState = { value: 2 };
  72. const cState = { value: 3 };
  73. const nestedState = { value: 4 };
  74. const mutations: Vuex.MutationTree<INumberState> = {
  75. INCREMENT (state: INumberState) {
  76. state.value = state.value + 1;
  77. }
  78. };
  79. const a = {
  80. state: aState,
  81. mutations
  82. };
  83. const b = {
  84. state: bState,
  85. mutations
  86. };
  87. const nested = {
  88. state: nestedState,
  89. mutations,
  90. modules: { a, b }
  91. };
  92. const c = {
  93. state: cState,
  94. mutations
  95. };
  96. const store = new Vuex.Store<IModuleState>({
  97. modules: { nested, c }
  98. });
  99. const valA = store.state.nested.a.value;
  100. const valB = store.state.nested.b.value;
  101. const valC = store.state.c.value;
  102. const valNested = store.state.nested.value;
  103. }
  104. namespace TestPlugin {
  105. const a = (store: Vuex.Store<any>) => {};
  106. const b = (store: Vuex.Store<ISimpleState>) => {};
  107. new Vuex.Store<ISimpleState>({
  108. state: { count: 1 },
  109. plugins: [a, b]
  110. });
  111. }
  112. namespace TestReplaceState {
  113. const store = createStore();
  114. store.replaceState({ count: 10 });
  115. }
  116. namespace TestWatch {
  117. const store = createStore();
  118. store.watch(state => state.count, value => {
  119. const a: number = value;
  120. }, {
  121. deep: true,
  122. immidiate: true
  123. });
  124. }
  125. namespace TestHotUpdate {
  126. const store = createStore();
  127. const mutations: Vuex.MutationTree<ISimpleState> = {
  128. INCREMENT (state: ISimpleState) {
  129. state.count++;
  130. }
  131. };
  132. store.hotUpdate({
  133. mutations
  134. });
  135. store.hotUpdate({
  136. modules: {
  137. nested: {
  138. state: { count: 0 },
  139. mutations,
  140. modules: {
  141. a: {
  142. state: { count: 1 },
  143. mutations
  144. },
  145. b: {
  146. state: { count: 2 },
  147. mutations
  148. }
  149. }
  150. },
  151. c: {
  152. state: { count: 4 },
  153. mutations
  154. }
  155. }
  156. });
  157. }
  158. namespace TestSubscribe {
  159. const store = createStore();
  160. const handler = (mutation: Vuex.MutationObject<any>, state: ISimpleState) => {
  161. state.count += 1;
  162. };
  163. const unsubscribe = store.subscribe(handler);
  164. unsubscribe();
  165. }
  166. namespace TestLogger {
  167. const logger = createLogger<ISimpleState>({
  168. collapsed: false,
  169. transformer: state => state.count,
  170. mutationTransformer: m => m
  171. });
  172. new Vuex.Store<ISimpleState>({
  173. state: { count: 1 },
  174. plugins: [logger]
  175. });
  176. }