index.ts 7.3 KB

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