index.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { InjectionKey } from "vue";
  2. import * as Vuex from "../index";
  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 key: InjectionKey<string> = Symbol('store')
  124. const storeWithKey = Vuex.useStore(key)
  125. storeWithKey.state.a
  126. const storeWithKeyString = Vuex.useStore('store')
  127. storeWithKeyString.state.a
  128. const storeWithState = Vuex.useStore<State>()
  129. storeWithState.state.a
  130. const storeAsAny = Vuex.useStore()
  131. storeAsAny.state.a
  132. }
  133. namespace RootModule {
  134. const store = new Vuex.Store({
  135. state: {
  136. value: 0
  137. },
  138. getters: {
  139. count: state => state.value,
  140. plus10: (_, { count }) => count + 10
  141. },
  142. actions: {
  143. foo ({ state, getters, dispatch, commit }, payload) {
  144. this.state.value;
  145. state.value;
  146. getters.count;
  147. dispatch("bar", {});
  148. commit("bar", {});
  149. }
  150. },
  151. mutations: {
  152. bar (state, payload) {}
  153. },
  154. strict: true,
  155. devtools: true
  156. });
  157. }
  158. namespace RootDefaultModule {
  159. const store = new Vuex.default.Store({
  160. state: {
  161. value: 0
  162. },
  163. getters: {
  164. count: state => state.value,
  165. plus10: (_, { count }) => count + 10
  166. },
  167. actions: {
  168. foo ({ state, getters, dispatch, commit }, payload) {
  169. this.state.value;
  170. state.value;
  171. getters.count;
  172. dispatch("bar", {});
  173. commit("bar", {});
  174. }
  175. },
  176. mutations: {
  177. bar (state, payload) {}
  178. },
  179. strict: true
  180. });
  181. }
  182. namespace InitialStateFunction {
  183. const store = new Vuex.Store({
  184. state: () => ({
  185. value: 1
  186. })
  187. });
  188. const n: number = store.state.value;
  189. }
  190. namespace NestedModules {
  191. interface RootState {
  192. a: {
  193. value: number;
  194. };
  195. b: {
  196. c: {
  197. value: number;
  198. };
  199. d: {
  200. value: number;
  201. },
  202. e: {
  203. value: number;
  204. }
  205. };
  206. }
  207. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  208. const module = {
  209. state: {
  210. value: 0
  211. },
  212. actions: {
  213. foo (
  214. { state, getters, dispatch, commit, rootState }: ActionStore,
  215. payload: { amount: number }
  216. ) {
  217. state.value;
  218. getters.root;
  219. rootState.b.c.value;
  220. dispatch("bar", {});
  221. commit("bar", payload);
  222. }
  223. },
  224. mutations: {
  225. bar (state: { value: number }, payload: { amount: number }) {
  226. state.value += payload.amount;
  227. }
  228. }
  229. };
  230. const store = new Vuex.Store<RootState>({
  231. getters: {
  232. root: state => state
  233. },
  234. modules: {
  235. a: module,
  236. b: {
  237. modules: {
  238. c: module,
  239. d: module,
  240. e: {
  241. state: {
  242. value: 0
  243. },
  244. actions: {
  245. foo(context: ActionStore, payload) {
  246. this.state.a;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. });
  254. }
  255. namespace NamespacedModule {
  256. const store = new Vuex.Store({
  257. state: { value: 0 },
  258. getters: {
  259. rootValue: state => state.value
  260. },
  261. actions: {
  262. foo () {}
  263. },
  264. mutations: {
  265. foo () {}
  266. },
  267. modules: {
  268. a: {
  269. namespaced: true,
  270. state: { value: 1 },
  271. actions: {
  272. test: {
  273. root: true,
  274. handler ({ dispatch }) {
  275. dispatch('foo')
  276. }
  277. },
  278. test2: {
  279. handler ({ dispatch }) {
  280. dispatch('foo')
  281. }
  282. }
  283. },
  284. modules: {
  285. b: {
  286. state: { value: 2 }
  287. },
  288. c: {
  289. namespaced: true,
  290. state: { value: 3 },
  291. getters: {
  292. constant: () => 10,
  293. count (state, getters, rootState, rootGetters) {
  294. getters.constant;
  295. rootGetters.rootValue;
  296. }
  297. },
  298. actions: {
  299. test ({ dispatch, commit, getters, rootGetters }) {
  300. getters.constant;
  301. rootGetters.rootValue;
  302. dispatch("foo");
  303. dispatch("foo", null, { root: true });
  304. commit("foo");
  305. commit("foo", null, { root: true });
  306. },
  307. foo () {}
  308. },
  309. mutations: {
  310. foo () {}
  311. }
  312. }
  313. }
  314. }
  315. }
  316. });
  317. }
  318. namespace RegisterModule {
  319. interface RootState {
  320. value: number;
  321. a?: {
  322. value: number;
  323. b?: {
  324. value: number;
  325. }
  326. };
  327. }
  328. const store = new Vuex.Store<RootState>({
  329. state: {
  330. value: 0
  331. }
  332. });
  333. store.registerModule("a", {
  334. state: { value: 1 }
  335. });
  336. store.hasModule('a')
  337. store.registerModule(["a", "b"], {
  338. state: { value: 2 }
  339. });
  340. store.registerModule(["a", "b"], {
  341. state: { value: 2 }
  342. }, { preserveState: true });
  343. store.hasModule(['a', 'b'])
  344. store.unregisterModule(["a", "b"]);
  345. store.unregisterModule("a");
  346. }
  347. namespace HotUpdate {
  348. interface RootState {
  349. value: number;
  350. a: {
  351. b: {
  352. value: number;
  353. };
  354. };
  355. };
  356. type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
  357. const getters = {
  358. rootValue: (state: RootState) => state.value
  359. };
  360. const actions = {
  361. foo (store: ActionStore, payload: number) {}
  362. };
  363. const mutations = {
  364. bar (state: { value: number }, payload: number) {}
  365. };
  366. const module = {
  367. state: {
  368. value: 0
  369. },
  370. getters: {
  371. count: (state: { value: number }) => state.value
  372. },
  373. actions,
  374. mutations
  375. };
  376. const modules = {
  377. a: {
  378. modules: {
  379. b: module
  380. }
  381. }
  382. };
  383. const store = new Vuex.Store<RootState>({
  384. state: {
  385. value: 0
  386. } as any,
  387. getters,
  388. actions,
  389. mutations,
  390. modules
  391. });
  392. store.hotUpdate({
  393. getters,
  394. actions,
  395. mutations,
  396. modules
  397. });
  398. }
  399. namespace Plugins {
  400. function plugin (store: Vuex.Store<{ value: number }>) {
  401. store.subscribe((mutation, state) => {
  402. mutation.type;
  403. state.value;
  404. });
  405. }
  406. class MyLogger {
  407. log(message: string) {
  408. console.log(message);
  409. }
  410. }
  411. const logger = Vuex.createLogger<{ value: number }>({
  412. collapsed: true,
  413. transformer: state => state.value,
  414. mutationTransformer: (mutation: { type: string }) => mutation.type,
  415. logger: new MyLogger()
  416. });
  417. const store = new Vuex.Store<{ value: number }>({
  418. state: {
  419. value: 0
  420. },
  421. plugins: [plugin, logger]
  422. });
  423. }