// import { Getters, Mutations, Actions, Module } from 'vuex-smart-module' // class RootState { count = 1 } // // RootState class RootGetters extends Getters<RootState> { get double() { // `state` return this.state.count * 2 } get triple() { // `getters` return this.getters.double + this.state.count } } // // , RootState class RootMutations extends Mutations<RootState> { increment(payload: number) { // `state` this.state.count += payload } } // // // , , class RootActions extends Actions< RootState, RootGetters, RootMutations, RootActions > { incrementAsync(payload: { amount: number; interval: number }) { // `state`, `getters`, `commit` `dispatch` return new Promise(resolve => { setTimeout(() => { this.commit('increment', payload.amount) }, payload.interval) }) } } // export default new Module({ state: RootState, getters: RootGetters, mutations: RootMutations, actions: RootActions })
import Vue from 'vue' import * as Vuex from 'vuex' import { createStore } from 'vuex-smart-module' import RootStore from './root' Vue.use(Vuex) export const store = createStore( RootStore, { strict: process.env.NODE_ENV !== 'production' } )
import FooStore from './modules/foo' /* … */ export default new Module({ state: RootState, getters: RootGetters, mutations: RootMutations, actions: RootActions, modules: { FooStore } })
import Vue from 'vue' // ( ) // import FooStore from '@/store/modules/foo' import RootStore from '@/store/root' export default Vue.extend({ computed: RootStore.mapGetters(['double']), methods: RootStore.mapActions({ incAsync: 'incrementAsync' }), created() { console.log(this.double) this.incAsync(undefined) } })
import { categories } from '@/api' export type Category { attributes: { hasPrice: boolean; icon: string lvl: number name: string slug: string }; id: number } export interface IParams { city_id: number } class AppState { categories: Category[] = [] } /* ... */ class AppMutations extends Mutations<AppState> { setCategories(categories: Category[]) { this.state.categories = categories } } class AppActions extends Actions< AppState, AppGetters, AppMutations, AppActions > { async getCategories({params}: {params: IParams}): Promise<Category[]> { return categories.get({params}).then( ({ data }: { data: Category[] }) => { this.commit("setCategories", data) return data } ) } }
import { Vue, Component } from "vue-property-decorator" // ( ) // import FooStore from '@/store/modules/foo' import RootStore from "@/store/root" // , , Typescript, : const Mappers = Vue.extend({ computed: { ...RootStore.mapGetters(["double"]) }, methods: { ...RootStore.mapActions({ incAsync: 'incrementAsync' }) } }); @Component export default class MyApp extends Mappers { created() { console.log(this.double) this.incAsync(undefined) } }
import { Store } from 'vuex' import { Getters, Actions, Module, Context } from 'vuex-smart-module' // import FooStore from './foo' /* … */ class BarGetters extends Getters { // foo!: Context<typeof FooStore>; // $init(store: Store<any>): void { // this.foo = FooStore.context(store) } get excited(): string { return this.foo.state.value + '!' // -> hello! } } /* … */
class FooState { /* ... */ } class FooMutations extends Mutations<FooState> { reset () { const s = new FooState() Object.keys(s).forEach(key => { this.state[key] = s[key] }) } }
Source: https://habr.com/ru/post/459050/
All Articles