index.ts 5.9 KB

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