【问题标题】:Cannot read property 'length' of undefined"无法读取未定义的属性“长度”
【发布时间】:2018-07-03 12:38:09
【问题描述】:

我收到以下错误。奇怪的是,我很肯定数据在那里,因为在我的 vue add on 中,我可以看到它成功地从 vuex 存储中获取信息。我最初的猜测是,在它创建模板时,数据还没有从商店中获取?

Vue warn]: Error in render: "TypeError: Cannot read property 'length' of undefined"

数据:'spaces'是从存储中抓取的。

    export default {
        name: "myspaces",
        data() {
            return {
                filterMaxLength: 3,
                selectedSpace: 0,
                selectedRoom: 0
            }
        },
        created() {
            // Default selected space (first in json)
            this.selectedSpace = this.spaces[0].id;

            // Default selected room (first in json)
            this.selectedRoom = this.spaces[0].rooms[0].id;
        },
        computed: {
            // Get 'spaces' from store.
            ...mapState([
                'spaces'
            ])
    }

模板:

<template>
      <div>  
         <v-flex v-if="spaces.length < filterMaxLength">
              <v-btn v-for="space in spaces">
                 <h4> {{space.name}} </h4>
              </v-btn>
         </v-flex>
     </div>
<template>

商店:

    import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        spaces:[
            {
                id:1,
                name:'House in Amsterdam',
                rooms:[
                    {
                        id:1,
                        name:'Bedroom Otto',
                    },
                    {
                        id:2,
                        name:'Bedroom Mischa'
                    }
                ]
            },
            {
                id:2,
                name:'Office in Amsterdam',
                rooms:[
                    {
                        id:1,
                        name:'Office 1',
                    },
                    {
                        id:2,
                        name:'Office 2'
                    }
                ]
            }
        ]} });

vue chrome add on 表示此信息在组件中:

【问题讨论】:

  • 向我们展示您的商店代码
  • 您的商店是否从外部来源获取空间?可能您只是缺少数据。
  • 我添加了商店,但是就像我试图用 vue 插件显示一样,它确实检测到组件页面上的数据。
  • spaces 在您的商店返回数据之前是未定义的,因此此时您无法读取其length。您只需要调整模板,使其可以在等待存储时处理未定义的数据; v-if="spaces &amp;&amp; spaces.length &lt; filterMaxLength" 可能就足够了。
  • @DanielBeck 嗨,Daniel,感谢您的回复,结果证明....同时将代码从旧文件复制并粘贴到我的新项目中。我忘了添加某些道具,例如room.images 等。因此这些道具是未定义的!所以问题解决了!

标签: vue.js vuejs2 vue-component


【解决方案1】:

始终在检查长度之前,确保您的属性设置好,然后检查长度

<v-flex v-if="spaces && spaces.length < filterMaxLength">

更新 ECMAScript 2020

您也可以为此使用Optional chaining

<v-flex v-if="spaces?.length < filterMaxLength">

【讨论】:

    【解决方案2】:

    你应该使用Object.keys(spaces).length,如:

    <template>
          <div>  
             <v-flex v-if="typeof spaces !== 'undefined' && typeof spaces === 'object' && Object.keys(spaces).length < filterMaxLength">
                  <v-btn v-for="space in spaces">
                     <h4> {{space.name}} </h4>
                  </v-btn>
             </v-flex>
         </div>
    <template>
    

    【讨论】:

      【解决方案3】:

      只是为了确保你在你的 vue 中有关注

      import { mapState } from "vuex";
      

      否则,您也可以使用 getter,例如。 :

      在你的 vue 文件中

      v-if="this.getSpaces.length !== 0"
      

      在你的 vue 文件的计算函数中

      getSpaces() {
      
        return this.$store.getters.getSpaces;
      
      }
      

      在您的商店中

      getters: {
          getSpaces: state => {
            return state.spaces;
          }
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-01
        • 1970-01-01
        • 2015-08-10
        • 2020-09-14
        • 2018-01-11
        • 2020-12-23
        • 2020-06-12
        • 2014-09-30
        相关资源
        最近更新 更多