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 Ts错误:声明类型既不是';无效';诺尔';任何';必须返回一个值。当return语句位于subscribe方法内时_Angular_Typescript_Firebase - Fatal编程技术网

Angular Ts错误:声明类型既不是';无效';诺尔';任何';必须返回一个值。当return语句位于subscribe方法内时

Angular Ts错误:声明类型既不是';无效';诺尔';任何';必须返回一个值。当return语句位于subscribe方法内时,angular,typescript,firebase,Angular,Typescript,Firebase,我在Angular 2项目中做了一个简单的服务,用于检查用户是否登录。它检查用户对象是否存在于FirebaseAuth对象中。但是,当我的返回语句实际位于auth变量的subscribe方法中时,函数声明会抛出一个“缺少返回语句”错误。代码如下所示: import { Component, OnInit , Injectable} from '@angular/core'; import { FirebaseAuthState, FirebaseAuth} from "angularfire2"

我在Angular 2项目中做了一个简单的服务,用于检查用户是否登录。它检查用户对象是否存在于FirebaseAuth对象中。但是,当我的返回语句实际位于auth变量的subscribe方法中时,函数声明会抛出一个“缺少返回语句”错误。代码如下所示:

import { Component, OnInit , Injectable} from '@angular/core';
import { FirebaseAuthState, FirebaseAuth} from "angularfire2";
@Injectable()
export class CheckLogged {

constructor(private auth:FirebaseAuth ){}
check(): boolean{
    this.auth.subscribe((user: FirebaseAuthState) => {
        if (user) {
            return true;  
        }
        return false;
    })
  }
}
“check():boolean”语句引发此错误

我在组件中的OnInit生命周期挂钩中调用我的函数 并将其赋给一个变量

this.loggedIn = this.CheckLogged.check();

然后,调用者需要订阅返回值。

工作起来很有魅力!非常感谢,伙计,不客气。很高兴听到你能让它工作:)这给了我一个错误“映射不是函数”。我需要执行与返回此.auth.pipe(map(..)等效的
操作。不确定原因,可能是为了避免与EcmaScript
map
冲突?请参阅RxJS 6中的可能更改。我自己还没有使用过它。
  check(): boolean{ // <<<== no boolean is returned from this function
    this.auth.subscribe((user: FirebaseAuthState) => {
        if (user) {
            return true;  
        }
        return false;
    })
  }
  check(): Observable<boolean>{ // <<<== no boolean is returned from this function
    return this.auth.map((user: FirebaseAuthState) => {
        if (user) {
            return true;  
        }
        return false;
    })
  }