index.ts 8.2 KB

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