index.ts 6.3 KB

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