学无先后,达者为师

网站首页 编程语言 正文

Cannot read property ‘forEach‘ of undefined

作者:小陈同学,, 更新时间: 2022-04-12 编程语言

Cannot read property 'forEach' of undefined

 

forEach ,用来遍历对象,如果为undefined,没有属性,就会错误。

解决思路:在遍历对象之前判断对象是否为空,然后再去遍历该对象。

 

  // 选择角色

  sel_role(item) {

    this.sel_role_id = item.r_id;

    this.sel_role_name = item.r_name;

    //遍历之前检查是否存在,否则会报错:Cannot read property 'forEach' of undefined

    if (!this.authority_list) {

      return

    }

    this.authority_list.forEach(element => {

      element['disabled'] = false;

      if (element.children) {

        element.children.forEach(child => {

          child['disabled'] = false;

        });

      }

    });

 

原文链接:https://blog.csdn.net/ping_lvy/article/details/116856039

栏目分类
最近更新