index.ts 7.1 KB

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