热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

vue组件中插值{{moustache}}无法使用数组的length属性?

大家好,我在使用vue时,组件的data中定义了一个JSON格式的对象

大家好,我在使用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呢?
谢谢。


   



推荐阅读
author-avatar
小辣椒jie
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有