index.ts 8.1 KB

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