如何从Vuetify列表VueJs中的Vuex存储中获取状态

时间:2018-07-13 10:10:31

标签: javascript vue.js vuex vuetify.js

我有一个Vue文件,看起来像这样:

import store from '@/store'
export default{
    name: 'myList',
    data: () => ({
        show: true,
        listContent: [{
                name: '1',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.one
                }
            }, {
                name: '2',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.two
                }
            }, {
                name: '3',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.three
                }
            }
        ]
    })
}

不起作用的部分是从“ listContent”中获取“值”。

 {
    name: '3',
    icon: 'person',
    value: function () {
        return store.state.myStore.settings.three
    }
 }

在我的代码中,我已导入视图,就好像要放入:

this.$store.state.myStore.settings.one

在值函数中,“ this”将引用对象

{
    name: '3',
    icon: 'person',
    value: function () {
        return store.state.myStore.settings.three
    }
}

我无法去商店。但是,我的代码仍然无法正常工作。我需要访问listContent内的商店。

列表的呈现方式如下:

<v-data-table :items="listContent" hide-actions hide-headers>
    <template slot="items" slot-scope="props">    
        <td>{{ props.item.name }}</td>
        <td class="text-xs-right" v-text="props.item.value()">  </td>
    </template>
</v-data-table>

我未正确引用商店,或模板不正确。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

为什么要让value是一个返回状态值的函数。您可以使用state

将其分配给this.$store.state.myStore.settings.one

为此,请将data选项设置为普通功能而不是箭头功能,以便this仍表示vue实例

export default {
  name: "myList",
  data() {
    return {
      show: true,
      listContent: [
        {
          name: "1",
          icon: "person",
          value: this.$store.state.myStore.settings.one
        },
        {
          name: "2",
          icon: "person",
          value: this.$store.state.myStore.settings.two
        },
        {
          name: "3",
          icon: "person",
          value: this.$store.state.myStore.settings.three
        }
      ]
    };
  }
};

答案 1 :(得分:0)

也许这会有所帮助。长了一个,但是行得通。

const myModule = {
  state: {
    test: "modulle",
    settings: {
      one: "This is one",
      two: "This is two",
      three: "This is three"
    }
  }
};

const store = new Vuex.Store({
  modules: { myModule }
});

new Vue({
  el: "#app",
  store,
  data() {
    return {
      listContent: [
        {
          name: "1",
          icon: "person",
          value: null
        },
        {
          name: "2",
          icon: "person",
          value: null
        },
        {
          name: "3",
          icon: "person",
          value: null
        }
      ]
    };
  },
  watch:{
    '$store.state.myModule.settings.one':{
        immediate:true,
      handler:function(value){
            this.listContent[0].value = value;
      }
    },
    '$store.state.myModule.settings.two':{
        immediate:true,
      handler:function(value){
            this.listContent[1].value = value;
      }
    },
    '$store.state.myModule.settings.three':{
        immediate:true,
      handler:function(value){
            this.listContent[2].value = value;
      }
    },
  }
});