代码之家  ›  专栏  ›  技术社区  ›  thatOneGuy

如何从vuefiy列表中的vuejs存储区获取状态

  •  1
  • thatOneGuy  · 技术社区  · 7 年前

    我有一个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”获取“value”。

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

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

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

    在value函数中,“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 回复  |  直到 7 年前
        1
  •  3
  •   Vamsi Krishna    7 年前

    你为什么要 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
            }
          ]
        };
      }
    };
    
        2
  •  0
  •   Ankit Kumar Ojha    7 年前

    也许这会有帮助。很长,但很管用。

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