index.ts 7.0 KB

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