【问题标题】:Nuxt nuxtServerInit now being called现在调用 Nuxt nuxtServerInit
【发布时间】:2020-06-23 01:52:38
【问题描述】:

我正在尝试在 Nuxt 中设置 store/index.js,但不明白为什么没有调用 nuxtServerInit。我已经添加了 console.log 来测试它,但它似乎没有工作或输出日志。

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

【问题讨论】:

    标签: vue.js vuex nuxt.js


    【解决方案1】:

    修好了!

    应用处于 spa 模式,而不是 universal

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2020-09-12
      • 2019-02-16
      相关资源
      最近更新 更多