NUXT中间件中未定义Vuex存储

Win*_*ale 8 javascript vue.js vuex nuxt.js

我正在练习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)
        })
}
Run Code Online (Sandbox Code Playgroud)

进入这里时store.commit('add'...会产生结果

无法读取未定义的属性'commit'

当我回显commit = undefined.

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

VUEX

存储/ index.js

import Vuex from 'vuex'

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

export default createStore
Run Code Online (Sandbox Code Playgroud)

Win*_*ale 13

我从上述教程的评论中找到了一个解决方案,但如果其他人也在努力,我想在这里分享.

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

然后重启你的开发服务器

npm run dev
Run Code Online (Sandbox Code Playgroud)

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


小智 8

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

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