Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 获取消息,说明函数的类型为void_Angular_Typescript - Fatal编程技术网

Angular 获取消息,说明函数的类型为void

Angular 获取消息,说明函数的类型为void,angular,typescript,Angular,Typescript,我对angular和任何试图为我正在进行的ionic项目设置授权模块的人都是新手 我正在接近,但是我创建的“auth”服务遇到了问题 我有一个具有以下登录方法的auth.service: login(user){ this.http.post(this.API_ENDPOINT+'/authenticate', user) .map(res => res.json()) .subscribe(user => { this.storeUser

我对angular和任何试图为我正在进行的ionic项目设置授权模块的人都是新手

我正在接近,但是我创建的“auth”服务遇到了问题

我有一个具有以下登录方法的auth.service:

  login(user){
    this.http.post(this.API_ENDPOINT+'/authenticate', user)
    .map(res => res.json())
    .subscribe(user => {
        this.storeUserCredentials(user); 
        return user;
    })
  }
在我的login.ts组件中,当我调用该方法时:

  constructor(public navCtrl: NavController, public navParams: NavParams, private auth: AuthenticationProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  userLogin(f){
    this.auth.login(f.value)
      .subscribe(response => {
      this.navCtrl.push('TabsPage');
    })
  }

}
我在“.subscribe”下看到一条红线,错误如下:

[ts] Property 'subscribe' does not exist on type 'void'.
any

我可能应该提到,我的目标是点击authenticate端点,然后处理“storeUserCredentials(),然后导航到tabs页面。

这是TypeScript错误。它表明存在人为错误。
login
方法不返回任何内容(
void
type)但预计将返回一个具有
subscribe
方法的可观察对象

在这种情况下,在
login
中调用subscribe是不可接受的,因为订阅应该发生在方法之外

如果
storeUserCredentials
是同步副作用,则可使用
do
运算符处理:

  login(user){
    this.http.post(this.API_ENDPOINT+'/authenticate', user)
    .map(res => res.json())
    .do(user => {
        this.storeUserCredentials(user); 
    })
  }

谢谢,estus。我刚刚了解了原因,因为我在服务中使用了.subscribe方法。因此,当我在login.ts组件中调用该函数时,我正在尝试应用.subscribe来订阅一种类型的订阅。我想我正在试图弄清楚如何处理storeUserCredentials()方法,然后在一切完成后从my login.ts组件导航到选项卡页面。获取以下内容:[ts]属性“do”在类型“Observable”上不存在。任何需要导入它的地方,就像任何其他RxJS操作符一样,都可以看到它工作了。因此在我的服务端,似乎所有内容都应该处理。现在,我的问题是在“this.auth.login(f.value)”完成处理(异步)后我的login.ts组件上,如何开始导航到下一页?在AngularJS中,我为我们提供了一个“then”函数。顺便说一句,感谢您的帮助!!我不使用ionic,但我想您已经在使用
this.navCtrl.push('TabsPage')来完成此操作了
subscribe
在这里扮演着与
相同的角色,然后
在这里。顺便说一句,你可以随时使用
http.post(…).toPromise()
切换到promises(),但我强烈建议不要这样做,因为观察到的存在是有原因的,并且可以做承诺做不到的事情。