vue改变data值但页面不刷新

有时候我们会通过异步请求来获取数据,此时我们直接通过this.data去赋值的话,会发现页面不能刷新dom,此时我们需要通过$set去给对象设置数据即可

that.$set(that.targetLevelTaskInfo, 'child2', res.data.data)

改之前:

this.targetLevelTaskInfo.child2 = res.data.data

改之后:

this.$ajax.get(url).then((res) => {
        if (res.data.data) {
          that.targetLevelTaskInfo = res.data.data
          that.$ajax.get(global.api.targetSuperviseInfoUrl+'?fatherId='+that.param.id+"&targetLevel=2").then(res => {
            if(res.data.status == 200){
              that.$set(that.targetLevelTaskInfo, 'child2', res.data.data)
              let child3Id = "";
              res.data.data.forEach(item=>{
                child3Id+=item.id+','
              })
              that.$ajax.get(global.api.targetSuperviseInfoUrl+'?fatherId='+child3Id+"&targetLevel=3").then(res => {
                if(res.data.status == 200){
                  that.$set(that.targetLevelTaskInfo, 'child3', res.data.data)
                  let child4Id = "";
                  res.data.data.forEach(item=>{
                    child4Id+=item.id+','
                  })
                  that.$ajax.get(global.api.targetSuperviseInfoUrl+'?fatherId='+child4Id+"&targetLevel=4").then(res => {
                    if(res.data.status == 200){
                      that.$set(that.targetLevelTaskInfo, 'child4', res.data.data)
                    }
                  })
                }
              })
            }
          })
        }
      });

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Vue中的`location.href`进行页面跳转时,如果当前页面的URL和跳转的URL是相同的,页面可能不会刷新。这是因为`location.href`只会在URL发生改变时才会重新加载页面。例如,如果你想从页面B跳转到页面A,并且它们的URL是相同的(例如都是https://www.baidu.com),那么页面可能不会刷新。 另外,当使用`location.href`进行页面跳转时,如果存在一个提交按钮并且在触发事件后会先提交表单,那么页面跳转可能会失效。解决这个问题的方法是将按钮的类型从`submit`改为`button`。 如果你希望在跳转后刷新页面,可以在使用`window.location.href`后再使用`window.location.reload()`方法,这样页面会重新加载。 综上所述,如果你在Vue中使用`location.href`进行跳转并希望刷新页面,你可以尝试以下解决方案: 1. 确认跳转的URL和当前页面的URL是否相同,如果相同,则可能不会刷新页面。 2. 如果存在一个提交按钮并且在触发事件后会先提交表单,考虑将按钮的类型改为`button`。 3. 在使用`window.location.href`后,使用`window.location.reload()`方法重新加载页面。 希望这些解决方案对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [使用window.location.href跳转页面刷新的问题](https://blog.csdn.net/u010856177/article/details/127306432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [window.location.href 跳转失效问题解决办法](https://blog.csdn.net/qq_39506434/article/details/130256273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值