index.ts 6.0 KB

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