index.ts 8.0 KB

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