后端服务器返回的数组(集合),vue+elementUI+axios前端拿到后不能遍历?,我明明赋值了啊怎么数组长度还是0?,别着急看过来一招教会你

相信有同学和我一样拿到服务器返回的数据明明可以在请求中遍历成功,怎么到了别的方法就不行了,这里我也查了很多资料,经过反复尝试还是失败了,不过我得到了启发然后成功了.

这是csdn别的同志写的,反正我是没有成功-------------------------------------

// this.userList=JSON.parse(JSON.stringify(res.data.data.userList))
// this.deptList=JSON.parse(JSON.stringify(res.data.data.deptList))

--------------------------------------------------------------------------------------------------------------------

    这是我用来存放信息的聊个数组
        data() {
            return {
                account:'',
                // 部门列表
                deptList:[],
                // 用户列表,[{},{}]
                userList:[]
            }
        }


    下面是我的请求以及我如何解决的

      // 获取通讯录保存到cookie
            getUserIds(){
                this.$axios.get('platform/api/contact/export', {
                    headers: {
                        'accessToken': localStorage.getItem('accessToken'),
                        'orgSecret': localStorage.getItem('orgSecret'),
                    }
                }).then(res => {
                       localStorage.setItem("userList",JSON.stringify(res.data.data.userList))
                    localStorage.setItem("deptList",JSON.stringify(res.data.data.deptList))
                    this.$notify({
                        title:"成功",
                        message:'获取通讯录成功',
                        type:"success"
                    })
                }).catch(err => {
                    this.$notify({
                        title:'失败',
                        message:'获取通讯录失败',
                        type:"error"
                    })
                })
            },


// 测试遍历通讯录
            testforeachUserList(){
                this.userList=JSON.parse(localStorage.getItem("userList"))
                for (let i=0;i<this.userList.length;i++){
                    console.log("======================循环内=======================")
                    console.log(this.userList[i].userId)
                }
                console.log("======================循环外=======================")
            }

 

 这是成功的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值