Aka*_*eth 9 vue.js vuex nuxt.js
我有以下文件,但找不到警告“商店/的经典模式已弃用,将在 Nuxt 3 中删除”的原因。一切正常,只是收到那个烦人的警告。
存储在 nuxt.js 中的modules/data.js文件。
const state = () => ({
loadedPosts: []
});
const mutations = {
setPosts(state, posts){
state.loadedPosts = posts;
}
};
const actions = {
setPosts(vuexContext, posts){
vuexContext.commit('setPosts', posts);
}
};
const getters = {
loadedPosts(state){
return state.loadedPosts;
}
};
export default {
state,
actions,
getters,
mutations
};
Run Code Online (Sandbox Code Playgroud)
存储在 nuxt.js 中的index.js文件。
import Vuex from 'vuex';
import data from "~/store/modules/data";
const createStore = () => {
return new Vuex.Store({
modules: {
data: {
namespaced: true,
...data
}
}
});
};
export default createStore;
Run Code Online (Sandbox Code Playgroud)
Aka*_*eth 21
最后,在阅读了大量答案和博客后,我找到了解决方案。
重要提示:-忘记了您对 vue.js 应用程序中的 vuex 模块的了解。在 nuxt.js 中使用 Vuex 有点不同。
解决方案:- Nuxt.js 让你有一个store目录,每个文件对应一个模块。只需直接在商店中添加必要的文件,您无需在商店中的“模块”目录中创建和添加文件。index.js文件是一个特殊文件,我们将在此处放置与模块无关的逻辑。
存储/data.js
export const state = () => ({
loadedPosts: []
});
export const mutations = {
setPosts(state, posts){
state.loadedPosts = posts;
}
};
export const actions = {
setPosts(vuexContext, posts){
vuexContext.commit('setPosts', posts);
}
};
export const getters = {
loadedPosts(state){
return state.loadedPosts;
}
};
Run Code Online (Sandbox Code Playgroud)
在项目中使用状态
this.$store.data.loadedPosts
Run Code Online (Sandbox Code Playgroud)
在项目中使用突变
this.$store.commit('data/setPosts', [{id: '1',...}, {id: '2',...}]);
Run Code Online (Sandbox Code Playgroud)
在项目中使用操作
this.$store.dispatch('data/setPosts', [{id: '1',...}, {id: '2',...}]);
Run Code Online (Sandbox Code Playgroud)
在项目中使用getter
this.$store.getters['data/loadedPosts'];
Run Code Online (Sandbox Code Playgroud)
重要参考资料:-
| 归档时间: |
|
| 查看次数: |
7216 次 |
| 最近记录: |