Angular2 cannot access 'this' inside a promise

前端 未结 3 1615
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 21:27

I am unable to call a function inside promise of ng2-sweetalert2 plugin

swal({
    title: \'Are you sure?\',
    text: \"You won\'t be able to revert this!\"         


        
3条回答
  •  轮回少年
    2021-01-05 21:51

    Assuming you're using TypeScript, you could use the arrow function expression, which preserves the value of this.

    swal({...}).then((x) => console.log(this)); // now 'this' is your component
    

提交回复
热议问题