index.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import * as Vuex from "../index";
  2. import createLogger from "../../dist/logger";
  3. namespace StoreInstance {
  4. const store = new Vuex.Store({
  5. state: {
  6. value: 0
  7. }
  8. });
  9. store.state.value;
  10. store.getters.foo;
  11. store.dispatch("foo", { amount: 1 }).then(() => {});
  12. store.dispatch({
  13. type: "foo",
  14. amount: 1
  15. }).then(() => {});
  16. store.commit("foo", { amount: 1 });
  17. store.commit({
  18. type: "foo",
  19. amount: 1
  20. });
  21. store.watch(state => state.value, value => {
  22. value = value + 1;
  23. }, {
  24. immediate: true,
  25. deep: true
  26. });
  27. store.subscribe((mutation, state) => {
  28. mutation.type;
  29. mutation.payload;
  30. state.value;
  31. });
  32. store.subscribeAction((action, state) => {
  33. action.type;
  34. action.payload;
  35. state.value;
  36. });
  37. store.subscribeAction({
  38. before(action, state) {
  39. action.type;
  40. action.payload;
  41. state.value;
  42. }
  43. });
  44. store.subscribeAction({
  45. before(action, state) {
  46. action.type;
  47. action.payload;
  48. state.value;
  49. },
  50. after(action, state) {
  51. action.type;
  52. action.payload;
  53. state.value;
  54. }
  55. });
  56. store.subscribeAction({
  57. after(action, state) {
  58. action.type;
  59. action.payload;
  60. state.value;
  61. }
  62. });
  63. store.replaceState({ value: 10 });
  64. }
  65. namespace RootModule {
  66. const store = new Vuex.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. devtools: true
  88. });
  89. }
  90. namespace RootDefaultModule {
  91. const store = new Vuex.default.Store({
  92. state: {
  93. value: 0
  94. },
  95. getters: {
  96. count: state => state.value,
  97. plus10: (_, { count }) => count + 10
  98. },
  99. actions: {
  100. foo ({ state, getters, dispatch, commit }, payload) {
  101. this.state.value;
  102. state.value;
  103. getters.count;
  104. dispatch("bar", {});
  105. commit("bar", {});
  106. }
  107. },
  108. mutations: {
  109. bar (state, payload) {}
  110. },
  111. strict: true
  112. });
  113. }
  114. namespace InitialStateFunction {
  115. const store = new Vuex.Store({
  116. state: () => ({
  117. value: 1
  118. })
  119. });
  120. const n: number = store.state.value;
  121. }
  122. namespace NestedModules {
  123. interface RootState {
  124. a: {
  125. value: number;
  126. };
  127. b: {
  128. c: {
  129. value: number;
  130. };
  131. d: {
  132. value: number;
  133. },
  134. e: {
  135. value: number;
  136. }
  137. };
  138. }
  139. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  140. const module = {
  141. state: {
  142. value: 0
  143. },
  144. actions: {
  145. foo (
  146. { state, getters, dispatch, commit, rootState }: ActionStore,
  147. payload: { amount: number }
  148. ) {
  149. state.value;
  150. getters.root;
  151. rootState.b.c.value;
  152. dispatch("bar", {});
  153. commit("bar", payload);
  154. }
  155. },
  156. mutations: {
  157. bar (state: { value: number }, payload: { amount: number }) {
  158. state.value += payload.amount;
  159. }
  160. }
  161. };
  162. const store = new Vuex.Store<RootState>({
  163. getters: {
  164. root: state => state
  165. },
  166. modules: {
  167. a: module,
  168. b: {
  169. modules: {
  170. c: module,
  171. d: module,
  172. e: {
  173. state: {
  174. value: 0
  175. },
  176. actions: {
  177. foo(context: ActionStore, payload) {
  178. this.state.a;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. });
  186. }
  187. namespace NamespacedModule {
  188. const store = new Vuex.Store({
  189. state: { value: 0 },
  190. getters: {
  191. rootValue: state => state.value
  192. },
  193. actions: {
  194. foo () {}
  195. },
  196. mutations: {
  197. foo () {}
  198. },
  199. modules: {
  200. a: {
  201. namespaced: true,
  202. state: { value: 1 },
  203. actions: {
  204. test: {
  205. root: true,
  206. handler ({ dispatch }) {
  207. dispatch('foo')
  208. }
  209. },
  210. test2: {
  211. handler ({ dispatch }) {
  212. dispatch('foo')
  213. }
  214. }
  215. },
  216. modules: {
  217. b: {
  218. state: { value: 2 }
  219. },
  220. c: {
  221. namespaced: true,
  222. state: { value: 3 },
  223. getters: {
  224. constant: () => 10,
  225. count (state, getters, rootState, rootGetters) {
  226. getters.constant;
  227. rootGetters.rootValue;
  228. }
  229. },
  230. actions: {
  231. test ({ dispatch, commit, getters, rootGetters }) {
  232. getters.constant;
  233. rootGetters.rootValue;
  234. dispatch("foo");
  235. dispatch("foo", null, { root: true });
  236. commit("foo");
  237. commit("foo", null, { root: true });
  238. },
  239. foo () {}
  240. },
  241. mutations: {
  242. foo () {}
  243. }
  244. }
  245. }
  246. }
  247. }
  248. });
  249. }
  250. namespace RegisterModule {
  251. interface RootState {
  252. value: number;
  253. a?: {
  254. value: number;
  255. b?: {
  256. value: number;
  257. }
  258. };
  259. }
  260. const store = new Vuex.Store<RootState>({
  261. state: {
  262. value: 0
  263. }
  264. });
  265. store.registerModule("a", {
  266. state: { value: 1 }
  267. });
  268. store.hasModule('a')
  269. store.registerModule(["a", "b"], {
  270. state: { value: 2 }
  271. });
  272. store.registerModule(["a", "b"], {
  273. state: { value: 2 }
  274. }, { preserveState: true });
  275. store.hasModule(['a', 'b'])
  276. store.unregisterModule(["a", "b"]);
  277. store.unregisterModule("a");
  278. }
  279. namespace HotUpdate {
  280. interface RootState {
  281. value: number;
  282. a: {
  283. b: {
  284. value: number;
  285. };
  286. };
  287. };
  288. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  289. const getters = {
  290. rootValue: (state: RootState) => state.value
  291. };
  292. const actions = {
  293. foo (store: ActionStore, payload: number) {}
  294. };
  295. const mutations = {
  296. bar (state: { value: number }, payload: number) {}
  297. };
  298. const module = {
  299. state: {
  300. value: 0
  301. },
  302. getters: {
  303. count: (state: { value: number }) => state.value
  304. },
  305. actions,
  306. mutations
  307. };
  308. const modules = {
  309. a: {
  310. modules: {
  311. b: module
  312. }
  313. }
  314. };
  315. const store = new Vuex.Store<RootState>({
  316. state: {
  317. value: 0
  318. } as any,
  319. getters,
  320. actions,
  321. mutations,
  322. modules
  323. });
  324. store.hotUpdate({
  325. getters,
  326. actions,
  327. mutations,
  328. modules
  329. });
  330. }
  331. namespace Plugins {
  332. function plugin (store: Vuex.Store<{ value: number }>) {
  333. store.subscribe((mutation, state) => {
  334. mutation.type;
  335. state.value;
  336. });
  337. }
  338. const logger = createLogger<{ value: number }>({
  339. collapsed: true,
  340. transformer: state => state.value,
  341. mutationTransformer: (mutation: { type: string }) => mutation.type
  342. });
  343. const store = new Vuex.Store<{ value: number }>({
  344. state: {
  345. value: 0
  346. },
  347. plugins: [plugin, logger]
  348. });
  349. }