Vue.js 现在正在调用Nuxt nuxtServerInit
我正在尝试在Nuxt中设置store/index.js,不明白为什么没有调用nuxtServerInit。我添加了console.log来测试它,但它似乎无法工作或输出日志Vue.js 现在正在调用Nuxt nuxtServerInit,vue.js,vuex,nuxt.js,Vue.js,Vuex,Nuxt.js,我正在尝试在Nuxt中设置store/index.js,不明白为什么没有调用nuxtServerInit。我添加了console.log来测试它,但它似乎无法工作或输出日志 import Vuex from 'vuex' import axios from 'axios' const createStore = () => { return new Vuex.Store({ state: { loadedPosts: []
import Vuex from 'vuex'
import axios from 'axios'
const createStore = () => {
return new Vuex.Store({
state: {
loadedPosts: []
},
mutations: {
setPosts(state, posts) {
state.loadedPosts = posts;
}
},
actions: {
nuxtServerInit(vuexContext, context) {
console.log('Init works!');
return axios.get("<firebase.link>")
.then(res => {
const postsArray = []
for (const key in res.data) {
postsArray.push({...res.data[key], id: key})
}
vuexContext.commit('setPosts', postsArray)
})
.catch(e => context.error(e))
},
setPosts(vuexContext, posts) {
vuexContext.commit('setPosts', posts)
}
},
getters: {
loadedPosts(state) {
console.log("Here we go",state.loadedPosts);
return state.loadedPosts
}
}
})
}
export default createStore
从“Vuex”导入Vuex
从“axios”导入axios
常量createStore=()=>{
返回新的Vuex.Store({
声明:{
加载的帖子:[]
},
突变:{
设置柱(状态,柱){
state.loadedPosts=posts;
}
},
行动:{
nuxtServerInit(vuexContext,上下文){
log('Init works!');
返回axios.get(“”)
。然后(res=>{
常量postsArray=[]
for(资源数据中的常量键){
push({…res.data[key],id:key})
}
vuexContext.commit('setPosts',postsArray)
})
.catch(e=>context.error(e))
},
设置帖子(vuexContext、帖子){
vuexContext.commit('setPosts',posts)
}
},
吸气剂:{
装载的邮件(状态){
log(“开始了”,state.loadedPosts);
返回state.loadedPosts
}
}
})
}
导出默认createStore
修复了它
该应用程序处于spa模式,而不是universal