Answer the question
In order to leave comments, you need to log in
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
well, get it from the contextasync fetchPosts( { commit, state }, page ) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question