赞
踩
1,store/index.js
- import Vue from 'vue';
- import Vuex from 'vuex'
- import home from './modules/home.js'
-
- Vue.use(Vuex);
- const store = new Vuex.Store({
- modules: {
- home
- }
- })
-
- export default store;
2,加命名空间
namespaced: true,
store/home/home.js
- const state = {
- indexData: {}
- }
- const mutations = {
- getIndexDataMutations(state, data) {
- state.indexData = data
- }
- }
- const actions = {
-
- }
- const getters = {}
- export default {
- //加上命名空间
- namespaced: true,
- state,
- mutations,
- actions,
- getters
- }
3,
只调用home.js下面的mutation方法怎么办?
this.$store.commit("模块名/该模块的方法",传递的参数)
4,在vue文件中调用
- created() {
- this.$store.commit("home/getIndexDataMutations", {
- name: 3
- })
- console.log(this.$store.state.home.indexData);
- },
控制台输出
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。