index.ts 7.1 KB

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