A
A
Artem Nanavov2020-01-12 15:56:06
Vue.js
Artem Nanavov, 2020-01-12 15:56:06

How to pass state to modul vuex?

I have a modul, and in it I turn to state, but he does not see it, I tried import state but still nothing
here is the code

export default{
    state: { 
        posts: [],
        perPage: 15,
        page: 1,
        total: 0,
        loading: false,
    },
    
    getters: {
        numPages: state => Math.ceil( state.total / state.perPage ),
       
    },
    
    mutations: {
        updateLoading: ( state, loading ) => state.loading = loading,
        updatePosts: ( state, { posts, total, page } ) => Object.assign( state, { posts, total, page } ),
    },
    
    actions: {
        async fetchPosts( { commit }, page ) {
            commit('updateLoading', true)
    
            const params = router.currentRoute

            const start = ( page - 1 ) * state.perPage;

            console.log( start )

            const new__url = `/api/photos?${ params.name }=true&limit=15`
    
            try {
                const response = await fetch( new__url )
                const posts = await response.json()
    
                const total = posts.totalItems
                commit( 'updatePosts', { posts, total, page }) 
            } catch (e) {
                console.error(e)
            }
    
            commit( 'updateLoading', false )
        },
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Pautov, 2020-01-12
@fertyga098

well, get it from the context
async fetchPosts( { commit, state }, page ) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question