在项目里曾对json用到长度length属性,在vue-cli的开发环境下正常无碍,万万没想到打包后放到服务器上全是报错,o(╥﹏╥)o。百度了一下方才发现原来json是没有长度属性的,-_-||。
在上线环境下获取json的长度网上的方法也是花样百出,这里我介绍一个特别好用还简单的原生方法,方法如下:
Object.keys(myObject).length
如此便可以愉快的使用json的长度啦:
getNew().then(res => {
this.newList = res.data.list1
let myObject = res.data.list1
this.newListLength = Object.keys(myObject).length
this.newListLength = Math.ceil((this.newListLength) / 4)
})