【问题标题】:Vuex store is undefined in NUXT middlewareNUXT 中间件中未定义 Vuex 存储
【发布时间】:2018-10-11 17:10:21
【问题描述】:

我正在练习 NUXT,并且从教程中它运行良好。进入NUXT中间件时我的失败。逻辑是如果页面重定向到其他页面,它将进入中间件并使用 axios 获取结果。

中间件/search.js

import axios from 'axios';

export default function ({ params, store }) {
    console.log(store)

    return axios.get(`https://itunes.apple.com/search?term=~${params.id}&entity=album`)
        .then((response) => {
            console.log(response.data.results);
            store.commit('add', response.data.results)
        })
}

在此处输入时,store.commit('add'... 将出现

无法读取未定义的属性“提交”

当我回显 commit = undefined。

我错过了什么?我已经尝试过this.$store.commit(...) 仍然未定义。

VUEX

store/index.js

import Vuex from 'vuex'

const createStore = () => {
  return new Vuex.Store({
    state: {
      albums: []
    },
    mutations: {
      add (state, payload) {
        state.albums = payload
      }
    }
  })
}

export default createStore

【问题讨论】:

  • 谢谢先生纠正我的问题
  • 不需要yenmar,它已经附加了。重启本地服务器解决了问题

标签: javascript vue.js vuex nuxt.js


【解决方案1】:

重新启动开发服务器也对我有用。进行更改时似乎没有重新加载 Vuex。

运行npm run dev,它应该可以工作。

【讨论】:

    【解决方案2】:

    我从上述教程的 cmets 中找到了解决方案,但如果其他人也遇到困难,我想在这里分享。

    停止你的开发服务器 ctrl+C

    然后重启你的开发服务器

    npm run dev
    

    那么现在在中间件 tnx 中会看到 VUEX

    【讨论】:

    • 开发服务器似乎没有重建 vue 存储,这让我摸不着头脑了几个小时...smh
    猜你喜欢
    • 2020-08-11
    • 2019-05-26
    • 2020-01-12
    • 2018-05-22
    • 2019-09-09
    • 2018-11-23
    • 2021-07-25
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多