功能如下:
这里就需要一开始只显示6个数据,点击展开才显示全部

HTML里调用showdetailList:

<div>
        <p v-for="(item, index) in showdetailList">
            <span>{{item.title}}</span>
            <span>{{item.name}}</span>
         </p>
</div>
<div v-if="detailList.length > 6" v-on:click="changeFoldState">
         <span>{{brandFold?'展开':'收起'}}</span>
 </div>

data数据:

data() {
      return {
brandFold: true
      }
}

moke一个数据:

mocData() {
    var items = ['Dr.Ci:Labo城野医生', '日本', '控油   收敛毛孔', '任何肤质', '面部', '化妆水', '任何肤质', '面部', '化妆水']
    var lists = ['商品品牌', '品牌国', '功能', '适合肤质', '适用部位', '产品类型', '商品品牌', '品牌国', '功能']
    for (var i = 0; i < items.length; i++) {
          let item = {title: lists[i], name: items[i]}
          this.detailList.push(item)
    }
    console.log(this.detailList)
}

使用computed属性:
set方法里的val值就是get方法返回的值

computed: {
   showdetailList: {
      get: function () {
         if (this.brandFold) {
             if (this.detailList.length < 7) {
                return this.detailList
             }
             let newArr = []
             for (var i = 0; i < 6; i++) {
                 let item = this.detailList[i]
                 newArr.push(item)
              }
              return newArr
           }
           return this.detailList
        },
        set: function (val) {
            this.showdetailList = val
        }
    }
}

changeFoldState方法:

changeFoldState() {
        this.brandFold = !this.brandFold
}
Logo

前往低代码交流专区

更多推荐