如何渲染存储在vuex中的数组

时间:2019-06-13 09:25:08

标签: vue.js vuex

我正在尝试以卡片形式呈现存储在商店中的对象数组。但是,我不能。由于它显示typeError。 声明

“渲染错误:“ TypeError:无法读取未定义的属性'item'”

我尝试使用此关键字并将代码移至mounted()挂钩。 但是,错误不断出现。

这是代码: CardRenderer.vue:

<template lang="html">

  <div>       
    <b-container class="bv-example-row">      
       <b-row v-for="(row, i) in this.  rows" v-bind:key="i">       
          <b-col v-for="(item, j) in row" v-bind:key="j" >

                    <!-- you card -->
              <b-card 
                :title="item.title" 
                img-src="item.icon" 
                img-alt="Image" 
                img-top 
                tag="article" 
                style="max-width: 20rem;" 
                class="mb-2"
              >
                <b-card-text>
                  <h1>{{item.name}}</h1>
                  <pre>{{item.description}}</pre>
                </b-card-text>
                  <b-button :href="'/dashboard/'+this.item.name" variant="primary">More</b-button>
              </b-card>                
          </b-col>
        </b-row>
    </b-container>    
  </div>

</template>

<script lang="js">
  export default  {
    name: 'CardRenderer',
    props: {

    },
     data() {
      return {
        // rows: []
      }
    },
    mounted() {

    },

    methods: {

    },
    computed: {
       rows() {
               const itemsPerRow = 3
                var rows = []
                let arr = this.$store.getters.responseAPI.apps
                // eslint-disable-next-line
                console.log(arr)
                for (let i = 0; i < arr.length; i += itemsPerRow){
                    let row = []
                    for (let z = 0; z < itemsPerRow; z++) {
                        row.push(arr[z])
                    }
                    rows.push(row)
                }


                // eslint-disable-next-line

                // console.log(this.rows) 
                return rows[0]
            }

    }
  }
</script>

<style scoped>

</style>

这是错误的样子。 enter image description here

这是行对象的样子 enter image description here

我如何消除错误并渲染卡。

我希望将更改后的代码作为答案。 谢谢:)

1 个答案:

答案 0 :(得分:1)

我认为您在计算所得的属性返回语句中有一个错误。

尝试将return rows[0]替换为return rows以返回数组,而不是第一项:)


我终于发现了一个错误)) 这是我在沙箱中的代码示例:https://codesandbox.io/embed/vue-template-sm0yx

您在模板上犯了一个错误,只需将this中的:href="'/dashboard/'+this.item.name"删除,使其看起来像这样::href="'/dashboard/'+item.name"

应该可以!))