vuex中显示子模块中的state要怎么写,通过...mapState的方式?

selectModule子模块(module)有一个state值是keyword,可以通过
this.$store.state.selectModule.keyword 找到;

现在我想通过下面...mapState的扩展方法来写,要怎么写呢?
keyword":"selectModule.keyword" 这样写不行,见下面代码注释;

select.vue

<script>
import {mapState,mapMutations,mapActions,mapGetters} from "vuex"
export default {
  name: 'app',
  data () {
    return {
    }
  },
  computed:{
    ...mapState(
        {
          //"keyword":"count"    //count可以显示
          "keyword":"selectModule.keyword"  //selectModule.keyword 显示不出来???
        }
      )
  },
  methods:{
      isShowFun:function(){
          this.$emit("isShowFun2");
      }
  },
  created:function(){
  }
}
</script>

store/index.js


import Vue from "vue"
import Vuex from "vuex"
import $ from "jquery"
Vue.use(Vuex);

var selectModule={
  state:{
    keyword:60
  },
  getters: {
    lists: function () {
      var url = "https://easy-mock.com/mock/5a61abf341d8910ea886ec50/searchLists";
      // return $.get(url,{},function(result){
      //   return result.lists;
      // })
      var result = {
        lists: [
          {
            title: "数据一"
          },
          {
            title: "数据二"
          },
          {
            title: "数据三"
          }
        ]
      }
      return result.lists;
    }
  },
    mutations:{
      changeKeyword:function(state,n){
        state.keyword=n;
      }
    },
    actions:{
      changeAction:function(context,n){
        context.commit("changeKeyword",n);
      }
    }
}

var store =new Vuex.Store({  //注意这里的Store是大写
  state:{
    count:100,
    count2:100
  },
  mutations:{
    addIncrement:function(state,n){
      setTimeout(function(){
        state.count+=n;
      },600)
    },
    reduce:function(state,n){
      setTimeout(function(){
        state.count-=n;
      },600)
    },
    addIncrement2:function(state,n){
        state.count2+=n;
    },
    reduce2:function(state,n){
        state.count2-=n;
    }
  },
  actions:{
    addAction:function(context,n){
      setTimeout(function(){
        context.commit("addIncrement2",n);
      },600)
    },
    reduceAction:function(context,n){
      setTimeout(function(){
        context.commit("reduce2",n);
      },600)
    }
  },
  getters:{
    limitNum:function(state){
      var a=state.count>120?120:state.count;
      return a;
    },
    limitNum2:function(state){
      var a=state.count2>120?120:state.count2;
      return a;
    }
  },
  modules:{
    selectModule
  }
})

export default store
阅读 13.1k
3 个回答
...mapState({
    //"keyword":"count"    //count可以显示
    "keyword": state => state.selectModule.keyword 
})

文档中有的

而且,你可以通过使用 createNamespacedHelpers 创建基于某个命名空间辅助函数。它返回一个对象,对象里有新的绑定在给定命名空间值上的组件绑定辅助函数:
import { createNamespacedHelpers } from 'vuex'

const { mapState, mapActions } = createNamespacedHelpers('some/nested/module')

export default {
  computed: {
    // 在 `some/nested/module` 中查找
    ...mapState({
      a: state => state.a,
      b: state => state.b
    })
  },
  methods: {
    // 在 `some/nested/module` 中查找
    ...mapActions([
      'foo',
      'bar'
    ])
  }
}
新手上路,请多包涵

`...mapState({


"keyword":'keyword '

})`
// 后面直接传字符串参数 'keyword'试试?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题