类型错误:无法读取未定义的属性“openModalDialog”

时间:2021-04-08 04:57:57

标签: typescript

这是我的 .ts 文件,每当条件失败并出现其他情况时,我想显示请求模式框,因此有一个函数 openModalDialog() ,但它显示未定义。

请帮我提前谢谢

import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
import { RequestModalComponent } from '../../request-modal/request-modal.component';
import axios from 'axios';

@Component({
 selector: 'app-shared-scheduled',
 templateUrl: './shared-scheduled.component.html',
 styleUrls: ['./shared-scheduled.component.css'],
})

  export class SharedOndatetimeScheduledComponent implements OnInit {
    bsModalRef: BsModalRef;
    constructor( private modalService: BsModalService ) { }

    ngOnInit(): void {
    } 

   openModalDialog() {
    console.log("callling this method.......")
     this.bsModalRef = this.modalService.show(RequestModalComponent);
   }

 navigateToTeleApp = () => {
   axios.get("*******").then(function (res) {
  console.log("response ...", res)
  if (res.data.message == "Not Found"){
    console.log("this condition....")
  }       
  else {
    console.log("this condition....222");
    this.openModalDialog();  
  }
  });
 }

1 个答案:

答案 0 :(得分:1)

我认为这是因为从轴获取方法,您使用函数关键字来处理回调并且它没有绑定,所以这个未定义尝试在那里使用 lamda 函数。

    navigateToTeleApp = () => {
// below line changed from function to lamda function
       axios.get("*******").then((res) => {
      console.log("response ...", res)
      if (res.data.message == "Not Found"){
        console.log("this condition....")
      }       
      else {
        console.log("this condition....222");
        this.openModalDialog();  
      }
      });
     }