【问题标题】:.then does not exist in type void in angular 2.then 在角度 2 中的 void 类型中不存在
【发布时间】:2017-08-01 00:40:51
【问题描述】:

我知道以下查询有可用的解决方案,但我是 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');
      }
    })
  }
}

【问题讨论】:

  • 您无需添加任何内容,只需将其绑定到变量即可。
  • passwordReset 显然不返回任何内容或声明了返回类型void(即使它返回某些内容)。请将代码作为文本添加到问题中,而不仅仅是屏幕截图的链接。
  • 谢谢你的回复这里是代码,

标签: angular typescript


【解决方案1】:

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

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')
      }
);

【讨论】:

    猜你喜欢
    • 2017-12-22
    • 2016-12-31
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2021-05-17
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多