index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.registerModule(["a", "b"], {
  183. state: { value: 2 }
  184. }, { preserveState: true });
  185. store.unregisterModule(["a", "b"]);
  186. store.unregisterModule("a");
  187. }
  188. namespace HotUpdate {
  189. interface RootState {
  190. value: number;
  191. a: {
  192. b: {
  193. value: number;
  194. };
  195. };
  196. };
  197. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  198. const getters = {
  199. rootValue: (state: RootState) => state.value
  200. };
  201. const actions = {
  202. foo (store: ActionStore, payload: number) {}
  203. };
  204. const mutations = {
  205. bar (state: { value: number }, payload: number) {}
  206. };
  207. const module = {
  208. state: {
  209. value: 0
  210. },
  211. getters: {
  212. count: (state: { value: number }) => state.value
  213. },
  214. actions,
  215. mutations
  216. };
  217. const modules = {
  218. a: {
  219. modules: {
  220. b: module
  221. }
  222. }
  223. };
  224. const store = new Vuex.Store<RootState>({
  225. state: {
  226. value: 0
  227. } as any,
  228. getters,
  229. actions,
  230. mutations,
  231. modules
  232. });
  233. store.hotUpdate({
  234. getters,
  235. actions,
  236. mutations,
  237. modules
  238. });
  239. }
  240. namespace Plugins {
  241. function plugin (store: Vuex.Store<{ value: number }>) {
  242. store.subscribe((mutation, state) => {
  243. mutation.type;
  244. state.value;
  245. });
  246. }
  247. const logger = createLogger<{ value: number }>({
  248. collapsed: true,
  249. transformer: state => state.value,
  250. mutationTransformer: (mutation: { type: string }) => mutation.type
  251. });
  252. const store = new Vuex.Store<{ value: number }>({
  253. state: {
  254. value: 0
  255. },
  256. plugins: [plugin, logger]
  257. });
  258. }