index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import Vue = require("vue");
  2. import * as Vuex from "../index";
  3. import createLogger from "../../dist/logger";
  4. Vue.use(Vuex);
  5. namespace StoreInstance {
  6. const store = new Vuex.Store({
  7. state: {
  8. value: 0
  9. }
  10. });
  11. store.state.value;
  12. store.getters.foo;
  13. store.dispatch("foo", { amount: 1 }).then(() => {});
  14. store.dispatch({
  15. type: "foo",
  16. amount: 1
  17. }).then(() => {});
  18. store.commit("foo", { amount: 1 }, { silent: true });
  19. store.commit({
  20. type: "foo",
  21. amount: 1
  22. }, { silent: true });
  23. store.watch(state => state.value, value => {
  24. value = value + 1;
  25. }, {
  26. immediate: true,
  27. deep: true
  28. });
  29. store.subscribe((mutation, state) => {
  30. mutation.type;
  31. state.value;
  32. });
  33. store.replaceState({ value: 10 });
  34. }
  35. namespace RootModule {
  36. const store = new Vuex.Store({
  37. state: {
  38. value: 0
  39. },
  40. getters: {
  41. count: state => state.value,
  42. plus10: (_, { count }) => count + 10
  43. },
  44. actions: {
  45. foo ({ state, getters, dispatch, commit }, payload) {
  46. state.value;
  47. getters.count;
  48. dispatch("bar", {});
  49. commit("bar", {}, { silent: true });
  50. }
  51. },
  52. mutations: {
  53. bar (state, payload) {}
  54. },
  55. strict: true
  56. });
  57. }
  58. namespace NestedModules {
  59. interface RootState {
  60. a: {
  61. value: number;
  62. };
  63. b: {
  64. c: {
  65. value: number;
  66. };
  67. d: {
  68. value: number;
  69. };
  70. };
  71. }
  72. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  73. const module = {
  74. state: {
  75. value: 0
  76. },
  77. actions: {
  78. foo (
  79. { state, getters, dispatch, commit, rootState }: ActionStore,
  80. payload: { amount: number }
  81. ) {
  82. state.value;
  83. getters.root;
  84. rootState.b.c.value;
  85. dispatch("bar", {});
  86. commit("bar", payload);
  87. }
  88. },
  89. mutations: {
  90. bar (state: { value: number }, payload: { amount: number }) {
  91. state.value += payload.amount;
  92. }
  93. }
  94. };
  95. const store = new Vuex.Store<RootState>({
  96. getters: {
  97. root: state => state
  98. },
  99. modules: {
  100. a: module,
  101. b: {
  102. modules: {
  103. c: module,
  104. d: module
  105. }
  106. }
  107. }
  108. });
  109. }
  110. namespace RegisterModule {
  111. interface RootState {
  112. value: number;
  113. a?: {
  114. value: number;
  115. b?: {
  116. value: number;
  117. }
  118. };
  119. }
  120. const store = new Vuex.Store<RootState>({
  121. state: {
  122. value: 0
  123. }
  124. });
  125. store.registerModule("a", {
  126. state: { value: 1 }
  127. });
  128. store.registerModule(["a", "b"], {
  129. state: { value: 2 }
  130. });
  131. store.unregisterModule(["a", "b"]);
  132. store.unregisterModule("a");
  133. }
  134. namespace HotUpdate {
  135. interface RootState {
  136. value: number;
  137. a: {
  138. b: {
  139. value: number;
  140. };
  141. };
  142. };
  143. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  144. const getters = {
  145. rootValue: (state: RootState) => state.value
  146. };
  147. const actions = {
  148. foo (store: ActionStore, payload: number) {}
  149. };
  150. const mutations = {
  151. bar (state: { value: number }, payload: number) {}
  152. };
  153. const module = {
  154. state: {
  155. value: 0
  156. },
  157. getters: {
  158. count: (state: { value: number }) => state.value
  159. },
  160. actions,
  161. mutations
  162. };
  163. const modules = {
  164. a: {
  165. modules: {
  166. b: module
  167. }
  168. }
  169. };
  170. const store = new Vuex.Store<RootState>({
  171. state: {
  172. value: 0
  173. } as any,
  174. getters,
  175. actions,
  176. mutations,
  177. modules
  178. });
  179. store.hotUpdate({
  180. getters,
  181. actions,
  182. mutations,
  183. modules
  184. });
  185. }
  186. namespace Plugins {
  187. function plugin (store: Vuex.Store<{ value: number }>) {
  188. store.subscribe((mutation, state) => {
  189. mutation.type;
  190. state.value;
  191. });
  192. }
  193. const logger = createLogger<{ value: number }>({
  194. collapsed: true,
  195. transformer: state => state.value,
  196. mutationTransformer: (mutation: { type: string }) => mutation.type
  197. });
  198. const store = new Vuex.Store<{ value: number }>({
  199. state: {
  200. value: 0
  201. },
  202. plugins: [plugin, logger]
  203. });
  204. }