美文网首页
vue展开收起,数据更新视图不更新--$set

vue展开收起,数据更新视图不更新--$set

作者: 华子_tm | 来源:发表于2019-07-17 09:14 被阅读0次

在开发过程中,遇到一种情况,请求到JSON数据后,往数据中新增属性,视图不更新。自己解决了半天,后来问了下人,让我去看看$set这个,我就去看了看官方文档。
根据官方文档定义:如果在实例创建之后添加新的属性到实例上,它不会触发视图更新。
受现代 JavaScript 的限制 (以及废弃 Object.observe),Vue 不能检测到对象属性的添加或删除。由于 Vue 会在初始化实例时对属性执行 getter/setter 转化过程,所以属性必须在 data 对象上存在才能让 Vue 转换它,这样才能让它是响应的。
计划做一个折叠开关,点击后折叠和打开。
下面看看代码:

<div v-if="tabindex != 0 && tabindex != 4">
        <div class="item_pzxx" v-for="(item, childrenIndex) in value.pcshList" :key="childrenIndex">
              <div class="pzxx_title" @click="pzxxsee(item, childrenIndex)">
                       <p>{{childrenIndex+1}}. 凭证编号:<span>{{item.VOUCHER_CODE}}</span></p>
                       <img v-if="item.pzxxshow" class="title_pic2" src="../../../assets/images/up.png" alt="">
                       <img v-else class="title_pic2" src="../../../assets/images/down.png" alt="">
              </div>
         <dir class="item_item item_border" v-show="item.pzxxshow">
                    .......
         </dir>
       </div>
    </div>
data () {
        return {
            henum: 0,
            starobjectarrnum: 0,
            objectArray: [],
        }
    },
created () {
        // 请求数据
        this.loadArticle()
    },
methods: {
        pzxxsee(item, childrenIndex) {
            item.pzxxshow = !item.pzxxshow
        },
      loadArticle () {
             let userdata = {
                        'ModuleName': 'asd',
                        'TokenId': window['cutModel'].TokenId,
                        'RequestType': 'asd',
                        'Data.DeptCode': thias.Dept_Code,
                        'Data.InDate': this.$route.query.time,
                        'Data.PageIndex': this.PageIndex,
                        'Data.InputName': this.value,
                        'Data.StartDate': this.startime,
                        'Data.EndDate': this.endtime,
                    }
                    this.$get('/asdasd/asdasd',userdata)
                    .then(res =>{
                        var res = JSON.parse(res)
                        // console.log(res)
                        if (res.Data.Statu_Code == 200) {
                            if (this.PageIndex == 1) {
                                this.henum = res.Data.In_Num
                                this.objectArray = res.Data.con_Detail_List
                                this.starobjectarrnum = res.Data.PageSize
                            } else {
                                this.objectArray = [
                                    ...this.objectArray,
                                    ...res.Data.con_Detail_List
                                ]
                            }
                            if(this.objectArray) {
                                // 折叠开关
                                this.objectArray.forEach(item => {
                                    for(let i = 0; i < item.pcshList.length; i++) {
                                        //这样写的话折叠开关不顶用,打印出来看到数据变化了,但是视图不更新
                                        // item.pcshList[i].pzxxshow = false
                                        //使用$set完美解决问题
                                        this.$set(item.pcshList[i],'pzxxshow', false)
                                    }
                                })
                            }
                        }
                    })
                    .catch(e => {
                        AlertModule.show({
                            title: '不好意思',
                            content: "服务器繁忙"
                        })
                    })
      }
}

解决了,折叠开关可以使用了。

相关文章

网友评论

      本文标题:vue展开收起,数据更新视图不更新--$set

      本文链接:https://www.haomeiwen.com/subject/qaeflctx.html