index.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 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. actions: {
  128. test: {
  129. root: true,
  130. handler ({ dispatch }) {
  131. dispatch('foo')
  132. }
  133. },
  134. test2: {
  135. handler ({ dispatch }) {
  136. dispatch('foo')
  137. }
  138. }
  139. },
  140. modules: {
  141. b: {
  142. state: { value: 2 }
  143. },
  144. c: {
  145. namespaced: true,
  146. state: { value: 3 },
  147. getters: {
  148. constant: () => 10,
  149. count (state, getters, rootState, rootGetters) {
  150. getters.constant;
  151. rootGetters.rootValue;
  152. }
  153. },
  154. actions: {
  155. test ({ dispatch, commit, getters, rootGetters }) {
  156. getters.constant;
  157. rootGetters.rootValue;
  158. dispatch("foo");
  159. dispatch("foo", null, { root: true });
  160. commit("foo");
  161. commit("foo", null, { root: true });
  162. },
  163. foo () {}
  164. },
  165. mutations: {
  166. foo () {}
  167. }
  168. }
  169. }
  170. }
  171. }
  172. });
  173. }
  174. namespace RegisterModule {
  175. interface RootState {
  176. value: number;
  177. a?: {
  178. value: number;
  179. b?: {
  180. value: number;
  181. }
  182. };
  183. }
  184. const store = new Vuex.Store<RootState>({
  185. state: {
  186. value: 0
  187. }
  188. });
  189. store.registerModule("a", {
  190. state: { value: 1 }
  191. });
  192. store.registerModule(["a", "b"], {
  193. state: { value: 2 }
  194. });
  195. store.registerModule(["a", "b"], {
  196. state: { value: 2 }
  197. }, { preserveState: true });
  198. store.unregisterModule(["a", "b"]);
  199. store.unregisterModule("a");
  200. }
  201. namespace HotUpdate {
  202. interface RootState {
  203. value: number;
  204. a: {
  205. b: {
  206. value: number;
  207. };
  208. };
  209. };
  210. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  211. const getters = {
  212. rootValue: (state: RootState) => state.value
  213. };
  214. const actions = {
  215. foo (store: ActionStore, payload: number) {}
  216. };
  217. const mutations = {
  218. bar (state: { value: number }, payload: number) {}
  219. };
  220. const module = {
  221. state: {
  222. value: 0
  223. },
  224. getters: {
  225. count: (state: { value: number }) => state.value
  226. },
  227. actions,
  228. mutations
  229. };
  230. const modules = {
  231. a: {
  232. modules: {
  233. b: module
  234. }
  235. }
  236. };
  237. const store = new Vuex.Store<RootState>({
  238. state: {
  239. value: 0
  240. } as any,
  241. getters,
  242. actions,
  243. mutations,
  244. modules
  245. });
  246. store.hotUpdate({
  247. getters,
  248. actions,
  249. mutations,
  250. modules
  251. });
  252. }
  253. namespace Plugins {
  254. function plugin (store: Vuex.Store<{ value: number }>) {
  255. store.subscribe((mutation, state) => {
  256. mutation.type;
  257. state.value;
  258. });
  259. }
  260. const logger = createLogger<{ value: number }>({
  261. collapsed: true,
  262. transformer: state => state.value,
  263. mutationTransformer: (mutation: { type: string }) => mutation.type
  264. });
  265. const store = new Vuex.Store<{ value: number }>({
  266. state: {
  267. value: 0
  268. },
  269. plugins: [plugin, logger]
  270. });
  271. }