0

각도 cli 앱에서 항목을 삭제하려고합니다. 감미로운 경고를 경고로 사용했으며 목록에서 항목을 삭제하려고합니다. 여기에 대한 코드가 있습니다. 이 코드는 타이프 스크립트 파일에 있습니다.각도 2 - 알림 호출 기능이 작동하지 않습니다.

import { AuthenticationService } from '../../../services/authentication.service'; 
declare var swal: any; 
export class AdminUsersComponent implements OnInit { 

    constructor(
     private authService: AuthenticationService, 
    ) { } 

    deleteUser(id) { 
     let userData = { 
      user_id: id 
     }; 
     swal({ 
      title: "Are you sure?", 
      text: "You will not be able to recover this!", 
      type: "warning", 
      showCancelButton: true, 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Yes, delete it!", 
      closeOnConfirm: false 
     }, function(){ 
      this.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     }); 

    } 
} 

문제는 내가 삭제를 확인하면 "this.authserivce"가 정의되지 않았다는 오류가 발생합니다. 내가 감미로운 경고를 확인으로 사용하지 않으면 제대로 작동합니다. 콜백 함수에서 매개 변수를 전달해야하지만 내가 통과해야하는지 모르겠다. 그럼 어떻게 해결할 수 있을까요?

+0

당신이 전체 코드를 보낼 수 있습니까? 'authService.deleteUser' 무엇입니까 – Manav

+0

@manav 코드를 업데이트했습니다. 보세요 – parth

+0

어쩌면 sweetalert가 '이'를 덮어 쓰시겠습니까? – Manav

답변

0

1 솔루션 : 사용 arrow function 때문에 자신의 this

swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }).then((result) => { 
     if (result.value) { 
      this.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     } 
    }); 

2 용액으로 함수식 바인드 this :

let that = this; 
swal({ 
     title: "Are you sure?", 
     text: "You will not be able to recover this!", 
     type: "warning", 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes, delete it!", 
     closeOnConfirm: false 
    }).then(function(result) { 
     if (result.value) { 
      that.authService.deleteUser(userData).subscribe(data => { 
       // response 
      }); 
     } 
    }); 
+1

고마워요. 지금 일하고있다. – parth