index.ts 8.2 KB

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