作者:小辣椒jie | 来源:互联网 | 2023-09-14 14:29
大家好,我在使用vue时,组件的data中定义了一个JSON格式的对象
1 2 3 4 5
| data(){
return {
arr : { "username": "#", "password": "#", "twis": [ { "date": 1487911092442, "content": "hello world", "username": "#" } ], "follow": [ "bbb", "ccc" ], "follwers": [] }
}
} |
对象里有几个数组我都定义到了computed属性里面
1 2 3 4 5 6 7 8 9 10 11
| computed: {
A:function(){
return this.arr.twis
},
B:function(){
return this.arr.follow
},
C:function(){
return this.arr.followers
}
}, |
我用插值的方式试图在模板中获取这些数组,都可以获取到,但是无法获取到数组的length属性:
1 2 3 4
| {{A}} //获取到twis数组 {{B}} //获取到follow数组 {{A.length}} //控制台报错,_vm.A is undefined {{B.length}} //控制台报错,_vm.B is undefined |
我尝试在定义计算属性时直接加上length,而不是在插值中引用它:
1 2 3 4 5
| computed: {
A:function(){
return this.arr.twis.length //这里我尝试直接获取数组长度
},
} |
然后在模板中引用插值A
1
| {{A}} // 控制台报错 this.arr.twis is undefined |
有明白的朋友希望可以指点一下为什么会出现这些报错,我要在页面中动态渲染出数组的长度,我怎样才能获取到数组的length呢?
谢谢。