.then在angular 2中的void类型中不存在

时间:2017-07-31 16:14:09

标签: angular typescript

我知道有一个解决方案可用于以下查询,但我是angular2中的菜鸟,并且不应该在void函数中添加什么,请帮助! enter image description here

export class PasswordResetPage {

  email : string;

  constructor(public navCtrl: NavController, 
              public navParams: NavParams , 
              public userservice :UserProvider,
              public AlertCtrl :AlertController) { }

  reset(){
    let alert = this.AlertCtrl.create({
      buttons :['ok']
    });
    this.userservice.passwordreset(this.email).then((res: any)=>{
      if(res.success){
        alert.setTitle('Email sent');
        alert.setSubTitle('please follow the instructions in the email
                           to reset the password')
      }
      else{
        alert.setTitle('failed');
      }
    })
  }
}

1 个答案:

答案 0 :(得分:0)

了解此服务调用的返回类型非常重要:

this.userservice.passwordreset(this.email)

.then仅在服务调用返回Promise时适用,并且它更有可能返回Observable,因此您需要订阅它以获取值。

this.userservice.passwordreset(this.email)
.subscribe(
      res => {
         alert.setTitle('Email sent');
         alert.setSubTitle('please follow the instructions in the email to reset the password')
      },
      err => {
          console.log('Error', err),
          alert.setTitle('failed')
      }
);