123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- import { InjectionKey } from "vue";
- import * as Vuex from "../index";
- namespace StoreInstance {
- const store = new Vuex.Store({
- state: {
- value: 0
- }
- });
- store.state.value;
- store.getters.foo;
- store.dispatch("foo", { amount: 1 }).then(() => {});
- store.dispatch({
- type: "foo",
- amount: 1
- }).then(() => {});
- store.commit("foo", { amount: 1 });
- store.commit({
- type: "foo",
- amount: 1
- });
- store.watch(state => state.value, value => {
- value = value + 1;
- }, {
- immediate: true,
- deep: true
- });
- store.subscribe((mutation, state) => {
- mutation.type;
- mutation.payload;
- state.value;
- });
- store.subscribe(() => {}, { prepend: true });
- store.subscribeAction((action, state) => {
- action.type;
- action.payload;
- state.value;
- });
- store.subscribeAction({
- before(action, state) {
- action.type;
- action.payload;
- state.value;
- }
- });
- store.subscribeAction({
- before(action, state) {
- action.type;
- action.payload;
- state.value;
- },
- after(action, state) {
- action.type;
- action.payload;
- state.value;
- }
- });
- store.subscribeAction({
- before(action, state) {
- action.type;
- action.payload;
- state.value;
- },
- error(action, state, error) {
- action.type;
- action.payload;
- state.value;
- error;
- }
- });
- store.subscribeAction({
- before(action, state) {
- action.type;
- action.payload;
- state.value;
- },
- after(action, state) {
- action.type;
- action.payload;
- state.value;
- },
- error(action, state, error) {
- action.type;
- action.payload;
- state.value;
- error;
- }
- });
- store.subscribeAction({
- after(action, state) {
- action.type;
- action.payload;
- state.value;
- }
- });
- store.subscribeAction({
- after(action, state) {
- action.type;
- action.payload;
- state.value;
- },
- error(action, state, error) {
- action.type;
- action.payload;
- state.value;
- error;
- }
- });
- store.subscribeAction({
- error(action, state, error) {
- action.type;
- action.payload;
- state.value;
- error;
- }
- });
- store.subscribeAction({}, { prepend: true });
- store.replaceState({ value: 10 });
- }
- namespace UseStoreFunction {
- interface State {
- a: string
- }
- const key: InjectionKey<string> = Symbol('store')
- const storeWithKey = Vuex.useStore(key)
- storeWithKey.state.a
- const storeWithKeyString = Vuex.useStore('store')
- storeWithKeyString.state.a
- const storeWithState = Vuex.useStore<State>()
- storeWithState.state.a
- const storeAsAny = Vuex.useStore()
- storeAsAny.state.a
- }
- namespace RootModule {
- const store = new Vuex.Store({
- state: {
- value: 0
- },
- getters: {
- count: state => state.value,
- plus10: (_, { count }) => count + 10
- },
- actions: {
- foo ({ state, getters, dispatch, commit }, payload) {
- this.state.value;
- state.value;
- getters.count;
- dispatch("bar", {});
- commit("bar", {});
- }
- },
- mutations: {
- bar (state, payload) {}
- },
- strict: true,
- devtools: true
- });
- }
- namespace RootDefaultModule {
- const store = new Vuex.default.Store({
- state: {
- value: 0
- },
- getters: {
- count: state => state.value,
- plus10: (_, { count }) => count + 10
- },
- actions: {
- foo ({ state, getters, dispatch, commit }, payload) {
- this.state.value;
- state.value;
- getters.count;
- dispatch("bar", {});
- commit("bar", {});
- }
- },
- mutations: {
- bar (state, payload) {}
- },
- strict: true
- });
- }
- namespace InitialStateFunction {
- const store = new Vuex.Store({
- state: () => ({
- value: 1
- })
- });
- const n: number = store.state.value;
- }
- namespace NestedModules {
- interface RootState {
- a: {
- value: number;
- };
- b: {
- c: {
- value: number;
- };
- d: {
- value: number;
- },
- e: {
- value: number;
- }
- };
- }
- type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
- const module = {
- state: {
- value: 0
- },
- actions: {
- foo (
- { state, getters, dispatch, commit, rootState }: ActionStore,
- payload: { amount: number }
- ) {
- state.value;
- getters.root;
- rootState.b.c.value;
- dispatch("bar", {});
- commit("bar", payload);
- }
- },
- mutations: {
- bar (state: { value: number }, payload: { amount: number }) {
- state.value += payload.amount;
- }
- }
- };
- const store = new Vuex.Store<RootState>({
- getters: {
- root: state => state
- },
- modules: {
- a: module,
- b: {
- modules: {
- c: module,
- d: module,
- e: {
- state: {
- value: 0
- },
- actions: {
- foo(context: ActionStore, payload) {
- this.state.a;
- }
- }
- }
- }
- }
- }
- });
- }
- namespace NamespacedModule {
- const store = new Vuex.Store({
- state: { value: 0 },
- getters: {
- rootValue: state => state.value
- },
- actions: {
- foo () {}
- },
- mutations: {
- foo () {}
- },
- modules: {
- a: {
- namespaced: true,
- state: { value: 1 },
- actions: {
- test: {
- root: true,
- handler ({ dispatch }) {
- dispatch('foo')
- }
- },
- test2: {
- handler ({ dispatch }) {
- dispatch('foo')
- }
- }
- },
- modules: {
- b: {
- state: { value: 2 }
- },
- c: {
- namespaced: true,
- state: { value: 3 },
- getters: {
- constant: () => 10,
- count (state, getters, rootState, rootGetters) {
- getters.constant;
- rootGetters.rootValue;
- }
- },
- actions: {
- test ({ dispatch, commit, getters, rootGetters }) {
- getters.constant;
- rootGetters.rootValue;
- dispatch("foo");
- dispatch("foo", null, { root: true });
- commit("foo");
- commit("foo", null, { root: true });
- },
- foo () {}
- },
- mutations: {
- foo () {}
- }
- }
- }
- }
- }
- });
- }
- namespace RegisterModule {
- interface RootState {
- value: number;
- a?: {
- value: number;
- b?: {
- value: number;
- }
- };
- }
- const store = new Vuex.Store<RootState>({
- state: {
- value: 0
- }
- });
- store.registerModule("a", {
- state: { value: 1 }
- });
- store.hasModule('a')
- store.registerModule(["a", "b"], {
- state: { value: 2 }
- });
- store.registerModule(["a", "b"], {
- state: { value: 2 }
- }, { preserveState: true });
- store.hasModule(['a', 'b'])
- store.unregisterModule(["a", "b"]);
- store.unregisterModule("a");
- }
- namespace HotUpdate {
- interface RootState {
- value: number;
- a: {
- b: {
- value: number;
- };
- };
- };
- type ActionStore = Vuex.ActionContext<{ value: number }, RootState>
- const getters = {
- rootValue: (state: RootState) => state.value
- };
- const actions = {
- foo (store: ActionStore, payload: number) {}
- };
- const mutations = {
- bar (state: { value: number }, payload: number) {}
- };
- const module = {
- state: {
- value: 0
- },
- getters: {
- count: (state: { value: number }) => state.value
- },
- actions,
- mutations
- };
- const modules = {
- a: {
- modules: {
- b: module
- }
- }
- };
- const store = new Vuex.Store<RootState>({
- state: {
- value: 0
- } as any,
- getters,
- actions,
- mutations,
- modules
- });
- store.hotUpdate({
- getters,
- actions,
- mutations,
- modules
- });
- }
- namespace Plugins {
- function plugin (store: Vuex.Store<{ value: number }>) {
- store.subscribe((mutation, state) => {
- mutation.type;
- state.value;
- });
- }
- class MyLogger {
- log(message: string) {
- console.log(message);
- }
- }
- const logger = Vuex.createLogger<{ value: number }>({
- collapsed: true,
- transformer: state => state.value,
- mutationTransformer: (mutation: { type: string }) => mutation.type,
- logger: new MyLogger()
- });
- const store = new Vuex.Store<{ value: number }>({
- state: {
- value: 0
- },
- plugins: [plugin, logger]
- });
- }
|