@fertyga098
FullStack js developer

Как обратиться к state из action?

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 )
        },
    }
}
  • Вопрос задан
  • 55 просмотров
Решения вопроса 1
bootd
@bootd
Гугли и ты откроешь врата знаний!
ну так из контекста получите его
async fetchPosts( { commit, state }, page ) {
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы