我正在练习 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'...
将产生
Cannot read property 'commit' of undefined
当我回显 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
最佳答案
我从上述教程的评论中找到了解决方案,但如果其他人也遇到困难,我想在这里分享。
停止你的开发服务器 ctrl+C
然后重启你的开发服务器
npm run dev
然后 VUEX 现在将在中间件 tnx 中看到
关于javascript - NUXT 中间件中未定义 Vuex store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120277/