函数调用不到,不执行的可能原因。

记录自己工作过程中遇到的比较难的或者不难但自己耗时很久的bug,以供以后反复记忆熟练学习,也可以给遇到相同问题的小伙伴提供一些些思路。

  1. 优化代码时,把代码提出来合并成一个方法时,在原来的地方调不到。当时解决思路是代码嵌套层太深了,调用不到,其实是没有传参,在原本代码处没有报错,但是在页面进来时报错了,其实是应该报错的。

先代码附上。

```methods: {
    nextBtnCancel() {
    
     ...省略部分代码

      if (check) {
        resultStr=resultStr+inputReason;
        let loading = this.$toast.loading({
          mask: true,
          message: "加载中...",
          duration: 4000,
        });
        let orderDataObj = {
          methodName: " ***  ",
          data: {
            orderid: this.$route.query.orderId,
            orgNo: this.$route.query.orgNo,
            endReason: resultStr,
          },
        }
        
        requestApi("  ",  ).then((res) => {
          console.log(res);
          if (res.code === 0 && res.data) {
       
            if (res.data === "1") {
              this.$dialog
                .confirm({
                  title: "",
                  message: "*****",
                  confirmButtonText: "确定取消",
                  cancelButtonText: "放弃取消",
                })
                .then(() => {
                        this.confirmCancel(resultStr,this.$route.query.orderId,this.$route.query.orgNo)
                })
                .catch(() => {
               
                  this.$router.push({
                    path: "/employeesPhyExam/myAppointment"
                  })
                });
            } else if (res.data === "0") { //预约订单未下载,直接调取消预约接口取消
              this.confirmCancel(resultStr,this.$route.query.orderId,this.$route.query.orgNo)
            }
          } else if (res.code === 1) {
            this.$toast(res.msg);
          }
        });
      }
    },
    confirmCancel(resultStr,orderid,orgNo){
      let cancelDataObj = {
        methodName: " ***",
        data: {
          orderid: orderid,
          orgNo:orgNo,
          endReason: resultStr
        },
      };
      requestApi('', ).then((response) => {
        if (response.code === 0) {  
          this.$dialog.alert({
            message: '取消成功',
          }).then(() => {
            this.$router.push({path: '/***/***'})
          })
        } else if (response.code === 1) { 
          this.$toast(response.msg)
        }
      })
    },
  },
};


# ```js
复制代码

this.confirmCancel( );最开始封装后我是直接在原来位置写这句调用的,代码没有报错,我以为就是嵌套太多层了,接口调用不到。其实不是。
总结思路:

  1. 遇到此类问题,因为没有报错,所以怀疑函数调用不到,或者是this ,that问题,可以打印this, 还有在外层 let that=this;打印that ,看看this和that是否一样,至于为什么还要继续研究,然后打印发现一样,并且里边的f里也有这个函数,说明可以调用的到,排除这个方法。
  2. 将函数在其他地方调用,比如页面刚进来时,mounted里,发现报错,用断点逐行调试,发现走到let obj后边就不走了,后来想到可能是Obj里的值可能取不到,要在函数里传过来。
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享