2017-02-09 85 views
0
{ 
    "data": { 
    "id": 3, 
    "name": "random", 
    "email": "[email protected]", 
    "created_at": "2017-02-06 18:16:06", 
    "updated_at": "2017-02-06 18:16:06", 
    "roles": [ 
     { 
     "id": 1, 
     "name": "ADMIN", 
     "access": { 
      "role4": true, 
      "role3": true, 
      "role2": true, 
      "role1": true 
     }, 
     "created_at": "-0001-11-30 00:00:00", 
     "updated_at": "-0001-11-30 00:00:00" 
     } 
    ] 
    } 
} 

我试图访问“访问”,并收到成效真/假,但它不工作Javascrypt(VueJS)对象与数组

currentUser.roles[0].access['role1']; 

错误:无法读取属性“0”的未定义

console.log(currentUser.roles[0].access['role1']); // returns true 

currentUser.roles // gives me all the roles 

数据通过Laravel Transformer传递到前端。

注意:使用VueJs,这里面的方法:

+0

是不是data.roles [0]。访问[ 'role1上']? – choasia

+0

不,它是undefined –

+0

我的意思是... currentUser从哪里来?我找不到它在你的对象 – choasia

回答

0

由于我没有看到整个代码,我可以假设。 首先,你必须与

this.currentUser = data //data that you retrieve 

比赛数据,然后访问:

this.currentUser.roles[0].access['role1'] 
0

VueComponent背景下,data属性散布到其this背景。因此:

this.roles[0].access['role1'] 
+0

但首先我需要引用当前登录的用户。这就是为什么目前用户。 –