收藏
回答

TypeError: Cannot read property '1' of undefined?

WXML:

<view class="menu_item {{index === currentIndex?'active':''}}"

    wx:for="{{leftmenulist}}"

    wx:for-index="index"

    wx:for-item="item"

    wx:key="*this"

    bindtap="handleItemTap"

    data-index="{{index}}"

    >

    {{item}}

    </view>

JS:

handleItemTap:function(e){

      var that = this

      const {index} = e.currentTarget.dataset;

      let rightcontent = that.cates[index];

      that.setData({

        currentIndex:index,

        // rightcontent:this.cates[index].children

      })

    },

问题描述:

在控制台打印console.log(e.currentTarget.dataset)出现的值会随着WXSS:wx:for-index = "index"中“index”的变化而变化

但是无法在that.cates[index]中使用

如果点击的目标的index为1

则提示错误

Error:

TypeError: Cannot read property '1' of undefined

将JS代码中的第四行改为

 let rightcontent = that.cates[0];

则会提示:

TypeError: Cannot read property '0' of undefined

求解答

回答关注问题邀请回答
收藏

1 个回答

  • 凉冰
    凉冰
    2022-02-20

    问题解决了,谢谢@Mr.Zhao 大佬的指点

    问题的原因是that.cates[index]无法调用页面下的data{}内的数组cates[],

    需要使用that.data.cates[index]才能正确调用

    2022-02-20
    有用
    回复
登录 后发表内容