首页 > 其他分享 >TypeError: Cannot read properties of undefined (reading '$modal')

TypeError: Cannot read properties of undefined (reading '$modal')

时间:2023-11-29 11:47:32浏览次数:34  
标签:TypeError undefined selfThis read response Vue modal id row

原代码:

    handleFinish(row) {this.$modal
        .confirm('确认录取学生编号为"' + row.stuCode + '"的成绩?')
        .then(function () {
          finishStudentScore({ id: row.id }).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            }
          });
        })
        .catch(() => {});
    }

在JavaScript中,this关键字指向当前执行上下文的对象。在Vue.js中,this通常指向当前Vue实例。
在上面的代码中,this引用了当前Vue实例,但是,在then回调函数内部,this的值可能会发生变化,不再指向Vue实例。

解决办法

法一:使用中间变量保存当前实例
为了避免这种情况,可以创建一个名为selfThis的变量,并将其赋值为this,以保存当前Vue实例的引用。使用selfThis可以确保在后续能够正常访问和使用Vue实例的属性和方法,即使this的值发生了改变。在异步操作中特别有用,因为异步操作的执行上下文可能与定义时的上下文不同。

   handleFinish(row) {
    let selfThis= this; this.$modal .confirm('确认录取学生编号为"' + row.stuCode + '"的成绩?') .then(function () { finishStudentScore({ id: row.id }).then((response) => { if (response.code == 200) { selfThis.$modal.msgSuccess("提交成功"); selfThis.open = false; selfThis.getList(); } }); }) .catch(() => {});
  }

法二:利用箭头函数解决(推荐)
在箭头函数中,this 指向的是定义时所在的上下文,而不是执行时所在的上下文。因此,使用箭头函数来定义 then 回调函数,可以避免 this 值的变化问题,也就不需要使用中间变量selfThis

    handleFinish(row) {
      this.$modal
        .confirm('确认录取学生编号为"' + row.stuCode + '"的成绩?')
        .then( () => {
          finishStudentScore({ id: row.id }).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            }
          });
        })
        .catch(() => {});
    }

 



 

标签:TypeError,undefined,selfThis,read,response,Vue,modal,id,row
From: https://www.cnblogs.com/gamepen/p/17864440.html

相关文章

  • 处理挖矿病毒kthreaddk的过程
    问题描述发现服务器的CPU和内存占用非常高,然后看了一下发现有几个异常的程序PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND12043root......
  • 使用emqttd时执行emqttd console时无反应或者报错Node undefined not responding to p
    1.无反应:  2.报错:Nodeundefinednotrespondingtopings. 解决办法:路径不能有空格,最好用存英文的路径。......
  • idea报错:XXX already exist in project. Please, specify another name.
    问题:idea报错:XXXalreadyexistinproject.Please,specifyanothername.并且左侧目录中并没有看见同名存在文件解决方法:1.打开File->ProjectStructure2.点击Modules->找到报错说存在的模块->点击减号删除->Apply->OK反思问题为什么存在应该是我在系统文件夹中之......
  • npm学习(九)之README.md文件
      包括文档(readme.md)npm建议您包含一个readme文件来记录您的包。自述文件必须有文件名readme.md。文件扩展名.md表示该文件是一个标记(markdown)文件。当有人发现您的包时,该文件将出现在npm网站上。在开始之前,请查看一些包页面,了解可以添加到readme文件中的信息,并了解为......
  • thread
    thread之前未解决的问题tp是什么?thethreadpointer,whichxv6usestoholdthiscore'shartid(corenumber),theindexintocpus[],实际上是存放着hartid的寄存器hartid是什么?运行当前代码硬件线程(hart)的ID。对软件层面来说,就是一个独立的处理器。但是实际上也有可能......
  • $(document).ready(function()
    页面加载完成后开始运行dostuffwhenDOMisready中的语句!$(document).ready(function(){//dostuffwhenDOMisready});$(“a”)是一个jquery的选择器(selector)$("")其中的字段就是元素的标记。比如$("div")就是<div></div>click是函数对象的一个方法。方法为点击鼠标事......
  • Thread RCP和NCP方案
    【技术专栏】泰凌微电子ThreadRCP和NCP方案介绍(一)https://mp.weixin.qq.com/s/Qn0WCaJCT5hJP9fgqB9o1g【技术专栏】泰凌微电子ThreadRCP和NCP方案介绍(二)https://mp.weixin.qq.com/s/gbQFljbzf7kLdh8HYYbPBA 【技术专栏】泰凌微电子ThreadRCP和NCP方案介绍(一)泰凌微电......
  • python threading线程数
    importthreadingimporttimename_list=[{"李四1":1234556},{"李四2":1234556},{"李四3":1234556},{"李四4":1234556},{"李四5":1234556},{"李四6":1234556},{"李四7"......
  • Fatal signal 11 (SIGSEGV) at 0x0000130f (code=1), thread xxx (Thread-xx)
    导致应用程序崩溃问题分析与解决:--复现--分析--解决最后先展示与问题相关的代码片:09-0413:26:32.826F/libc(572):Fatalsignal11(SIGSEGV)at0x0000130f(code=1),xxxx844(Thread-46)09-0413:26:32.936I/DEBUG(103):*************************......
  • 报错:undefined reference to `WinMain'
    报错:undefinedreferenceto`WinMain'错音是编译器找不到main()函数:可能缺少是main()函数,比如main拼写错误可能是main()函数不再全局命名空间中,注意main()函数必须置于默认命名空间(即全局命名空间)下......