서버 측 렌더링 프로젝트에 nuxtjs를 사용하고 있습니다. Vuex 저장소를 업데이트하고 페이지를 표시 할 위치에서 REST API를 가지고 있습니다. In the nuxtjs documentation for using Vuex store, it mentions using fetch api of the pages. 그래서 API에 대한 기사 목록을 가져 와서 페이지를 렌더링하려고합니다.Nuxtjs vuex 저장소가 업데이트 된 경우에도 페이지 구성 요소가 업데이트되지 않습니다.
Vuex 가기 : 페이지/index.vue에서
const createStore =() => {
return new Vuex.Store({
state: {
article_list: []
},
mutations: {
updateArticleList (state, payload) {
state.article_list = payload
console.log('article list length', state.article_list.length)
}
},
actions: {
mutateArticleList ({commit}, payload) {
api.get_articles()
.then((data) => {
commit('updateArticleList', data)
})
}
}
})
}
:
<template>
<div>
<button @click="update">Update</button>
<p v-for="article in articles" :key="article.id">Article</p>
</div>
</template>
<script>
export default {
fetch ({store}) {
store.dispatch('mutateArticleList')
},
computed: {
articles() {
return this.$store.state.article_list
}
},
methods: {
update() {
this.$store.dispatch('mutateArticleList')
}
}
}
</script>
콘솔에 내가 가게의 article_list가 업데이트 된 것을 볼 수 있습니다
console.log('article list length', state.article_list.length) // 16
그러나 index.vue 페이지에는 기사가 없습니다. 심지어 인덱스 created
방법의 콘솔에서 article_list 내가 여기 실종 무엇 0
console.log('index created length', this.$store.state.article_list.length) // 0
입니까? 저 좀 도와 주 시겠어요.
안녕 index.vue! 약간의 수정으로 내 질문을 코드로 업데이트했습니다. 그러나 여전히 페이지는 업데이트되지 않습니다. 터미널에서 총 기사를 볼 수 있지만 브라우저에서 기사 길이는 0입니다. API에서 항목을 가져 오기위한 작업을 업데이트/발송하기위한 추가 단추가 추가되었습니다. 항목을 가져 오기 위해 버튼을 클릭하면 페이지가 업데이트됩니다. API 결과를 얻은 다음 페이지를 표시하려면 어떻게해야합니까? – Robin
@Robin created() 훅에서 update 메소드를 호출하고 가져 오는 모든 것을 제거하십시오. –