index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 });
  19. store.commit({
  20. type: "foo",
  21. amount: 1
  22. });
  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", {});
  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 NamespacedModule {
  111. const store = new Vuex.Store({
  112. state: { value: 0 },
  113. getters: {
  114. rootValue: state => state.value
  115. },
  116. actions: {
  117. foo () {}
  118. },
  119. mutations: {
  120. foo () {}
  121. },
  122. modules: {
  123. a: {
  124. namespace: "prefix/",
  125. state: { value: 1 },
  126. modules: {
  127. b: {
  128. state: { value: 2 }
  129. },
  130. c: {
  131. namespace: "nested/",
  132. state: { value: 3 },
  133. getters: {
  134. constant: () => 10,
  135. count (state, getters, rootState, rootGetters) {
  136. getters.constant;
  137. rootGetters.rootValue;
  138. }
  139. },
  140. actions: {
  141. test ({ dispatch, commit, getters, rootGetters }) {
  142. getters.constant;
  143. rootGetters.rootValue;
  144. dispatch("foo");
  145. dispatch("foo", null, { root: true });
  146. commit("foo");
  147. commit("foo", null, { root: true });
  148. },
  149. foo () {}
  150. },
  151. mutations: {
  152. foo () {}
  153. }
  154. }
  155. }
  156. }
  157. }
  158. });
  159. }
  160. namespace RegisterModule {
  161. interface RootState {
  162. value: number;
  163. a?: {
  164. value: number;
  165. b?: {
  166. value: number;
  167. }
  168. };
  169. }
  170. const store = new Vuex.Store<RootState>({
  171. state: {
  172. value: 0
  173. }
  174. });
  175. store.registerModule("a", {
  176. state: { value: 1 }
  177. });
  178. store.registerModule(["a", "b"], {
  179. state: { value: 2 }
  180. });
  181. store.unregisterModule(["a", "b"]);
  182. store.unregisterModule("a");
  183. }
  184. namespace HotUpdate {
  185. interface RootState {
  186. value: number;
  187. a: {
  188. b: {
  189. value: number;
  190. };
  191. };
  192. };
  193. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  194. const getters = {
  195. rootValue: (state: RootState) => state.value
  196. };
  197. const actions = {
  198. foo (store: ActionStore, payload: number) {}
  199. };
  200. const mutations = {
  201. bar (state: { value: number }, payload: number) {}
  202. };
  203. const module = {
  204. state: {
  205. value: 0
  206. },
  207. getters: {
  208. count: (state: { value: number }) => state.value
  209. },
  210. actions,
  211. mutations
  212. };
  213. const modules = {
  214. a: {
  215. modules: {
  216. b: module
  217. }
  218. }
  219. };
  220. const store = new Vuex.Store<RootState>({
  221. state: {
  222. value: 0
  223. } as any,
  224. getters,
  225. actions,
  226. mutations,
  227. modules
  228. });
  229. store.hotUpdate({
  230. getters,
  231. actions,
  232. mutations,
  233. modules
  234. });
  235. }
  236. namespace Plugins {
  237. function plugin (store: Vuex.Store<{ value: number }>) {
  238. store.subscribe((mutation, state) => {
  239. mutation.type;
  240. state.value;
  241. });
  242. }
  243. const logger = createLogger<{ value: number }>({
  244. collapsed: true,
  245. transformer: state => state.value,
  246. mutationTransformer: (mutation: { type: string }) => mutation.type
  247. });
  248. const store = new Vuex.Store<{ value: number }>({
  249. state: {
  250. value: 0
  251. },
  252. plugins: [plugin, logger]
  253. });
  254. }