【问题标题】:How to get states from a Vuex store from within a Vuetify list, VueJs如何从 Vuetify 列表中的 Vuex 商店获取状态,VueJs
【发布时间】:2018-12-21 16:41:24
【问题描述】:

我有一个看起来像这样的 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>

要么我错误地引用了商店,要么模板不正确。有什么想法吗?

【问题讨论】:

  • 商店包含模块,我想从中获取的模块是名称'myStore'
  • 您希望这些值具有反应性吗?

标签: javascript vue.js vuex vuetify.js


【解决方案1】:

为什么要value 是一个返回状态值的函数。您可以使用 this.$store.state.myStore.settings.one 将其分配给 state

为此,将 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
        }
      ]
    };
  }
};

【讨论】:

  • @thatOneGuy 它确实有效。请参阅此fiddle。您是否将模块注册为数组或对象?
  • @VamsiKrishna 查看日志,未定义
  • @AnkitKumarOjha 这只是一个测试。忘了删除。但看输出
  • 对于箭头函数,这是上下文相关的。
  • @thatOneGuy 我在回答中提到过。箭头函数在词法上绑定this。 Vue 已经注意将this 绑定到 vue 实例,因此请使用普通函数。不仅适用于data,还适用于methodscomputedwatchers
【解决方案2】:

也许这会有所帮助。很长,但它有效。

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;
      }
    },
  }
});

【讨论】:

  • 设置观察者是多余的
猜你喜欢
  • 2018-12-21
  • 2017-07-06
  • 2018-11-30
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多