index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. mutation.payload;
  32. state.value;
  33. });
  34. store.replaceState({ value: 10 });
  35. }
  36. namespace RootModule {
  37. const store = new Vuex.Store({
  38. state: {
  39. value: 0
  40. },
  41. getters: {
  42. count: state => state.value,
  43. plus10: (_, { count }) => count + 10
  44. },
  45. actions: {
  46. foo ({ state, getters, dispatch, commit }, payload) {
  47. state.value;
  48. getters.count;
  49. dispatch("bar", {});
  50. commit("bar", {});
  51. }
  52. },
  53. mutations: {
  54. bar (state, payload) {}
  55. },
  56. strict: true
  57. });
  58. }
  59. namespace NestedModules {
  60. interface RootState {
  61. a: {
  62. value: number;
  63. };
  64. b: {
  65. c: {
  66. value: number;
  67. };
  68. d: {
  69. value: number;
  70. };
  71. };
  72. }
  73. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  74. const module = {
  75. state: {
  76. value: 0
  77. },
  78. actions: {
  79. foo (
  80. { state, getters, dispatch, commit, rootState }: ActionStore,
  81. payload: { amount: number }
  82. ) {
  83. state.value;
  84. getters.root;
  85. rootState.b.c.value;
  86. dispatch("bar", {});
  87. commit("bar", payload);
  88. }
  89. },
  90. mutations: {
  91. bar (state: { value: number }, payload: { amount: number }) {
  92. state.value += payload.amount;
  93. }
  94. }
  95. };
  96. const store = new Vuex.Store<RootState>({
  97. getters: {
  98. root: state => state
  99. },
  100. modules: {
  101. a: module,
  102. b: {
  103. modules: {
  104. c: module,
  105. d: module
  106. }
  107. }
  108. }
  109. });
  110. }
  111. namespace NamespacedModule {
  112. const store = new Vuex.Store({
  113. state: { value: 0 },
  114. getters: {
  115. rootValue: state => state.value
  116. },
  117. actions: {
  118. foo () {}
  119. },
  120. mutations: {
  121. foo () {}
  122. },
  123. modules: {
  124. a: {
  125. namespaced: true,
  126. state: { value: 1 },
  127. modules: {
  128. b: {
  129. state: { value: 2 }
  130. },
  131. c: {
  132. namespaced: true,
  133. state: { value: 3 },
  134. getters: {
  135. constant: () => 10,
  136. count (state, getters, rootState, rootGetters) {
  137. getters.constant;
  138. rootGetters.rootValue;
  139. }
  140. },
  141. actions: {
  142. test ({ dispatch, commit, getters, rootGetters }) {
  143. getters.constant;
  144. rootGetters.rootValue;
  145. dispatch("foo");
  146. dispatch("foo", null, { root: true });
  147. commit("foo");
  148. commit("foo", null, { root: true });
  149. },
  150. foo () {}
  151. },
  152. mutations: {
  153. foo () {}
  154. }
  155. }
  156. }
  157. }
  158. }
  159. });
  160. }
  161. namespace RegisterModule {
  162. interface RootState {
  163. value: number;
  164. a?: {
  165. value: number;
  166. b?: {
  167. value: number;
  168. }
  169. };
  170. }
  171. const store = new Vuex.Store<RootState>({
  172. state: {
  173. value: 0
  174. }
  175. });
  176. store.registerModule("a", {
  177. state: { value: 1 }
  178. });
  179. store.registerModule(["a", "b"], {
  180. state: { value: 2 }
  181. });
  182. store.unregisterModule(["a", "b"]);
  183. store.unregisterModule("a");
  184. }
  185. namespace HotUpdate {
  186. interface RootState {
  187. value: number;
  188. a: {
  189. b: {
  190. value: number;
  191. };
  192. };
  193. };
  194. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  195. const getters = {
  196. rootValue: (state: RootState) => state.value
  197. };
  198. const actions = {
  199. foo (store: ActionStore, payload: number) {}
  200. };
  201. const mutations = {
  202. bar (state: { value: number }, payload: number) {}
  203. };
  204. const module = {
  205. state: {
  206. value: 0
  207. },
  208. getters: {
  209. count: (state: { value: number }) => state.value
  210. },
  211. actions,
  212. mutations
  213. };
  214. const modules = {
  215. a: {
  216. modules: {
  217. b: module
  218. }
  219. }
  220. };
  221. const store = new Vuex.Store<RootState>({
  222. state: {
  223. value: 0
  224. } as any,
  225. getters,
  226. actions,
  227. mutations,
  228. modules
  229. });
  230. store.hotUpdate({
  231. getters,
  232. actions,
  233. mutations,
  234. modules
  235. });
  236. }
  237. namespace Plugins {
  238. function plugin (store: Vuex.Store<{ value: number }>) {
  239. store.subscribe((mutation, state) => {
  240. mutation.type;
  241. state.value;
  242. });
  243. }
  244. const logger = createLogger<{ value: number }>({
  245. collapsed: true,
  246. transformer: state => state.value,
  247. mutationTransformer: (mutation: { type: string }) => mutation.type
  248. });
  249. const store = new Vuex.Store<{ value: number }>({
  250. state: {
  251. value: 0
  252. },
  253. plugins: [plugin, logger]
  254. });
  255. }