angular 2、return 根据if语句的Observable

angular 2, return Observables according to if statement

嗨,我想创建这样的东西。

someFunction():Observable<Response>{
  let response$ = new Observerable();

  if (...){
    response$ = this.http.get(url1)
  }else{
    response$ = this.http.get(url2)
  }
  return response$
}

如果我订阅它,我会收到一个错误

Cannot read property 'subscribe' of undefined

提前感谢大家的帮助!

编辑---------- 这是使用 ionic 2 和 cordova 的实际功能。 它检查该应用程序是否在移动设备上,并打开一个带有 url 的弹出窗口(打算用于社交登录)

一旦 window 关闭,我将需要执行 http.get 请求以从我的后端获取令牌。

public social(providerName:string):Observable<Response>{

let response$ = new Observable<Response>();
let url =providerName+'/someUrl'

let authWindow;

if(window.cordova){
    let authWindow= InAppBrowser.open(url, '_blank', 'location=no');

    authWindow.addEventListener('loadstop', event => {
        if(event.url.indexOf(`${providerName}/callback`) > -1){
            authWindow.close()
        }
    });

    authWindow.addEventListener('loaderror', event =>{
        if(event.url.indexOf(`${providerName}/callback`) > -1){
            authWindow.close()
        }
    });

    authWindow.addEventListener('exit', () => {
        setTimeout(()=>{
             response$ = this.http.get(...)
        },500)
    })

}else{
    authWindow = createWindow(popupUrl);
    let window = Observable.interval(1000);

    let  pingWindow = window.subscribe(
        ()=>{
            if(authWindow.closed){
                pingWindow.unsubscribe();

                response$ = this.http.get(...)

            }
        })
}

return response$

}

更新揭示了问题。
您不是直接分配给 response$ 而是在异步回调中。 return response$执行时,none个赋值给response$

    setTimeout(()=>{
         response$ = this.http.get(...)
    },500)

也不

let  pingWindow = window.subscribe(
    ()=>{
        if(authWindow.closed){
            pingWindow.unsubscribe();

            response$ = this.http.get(...)

        }
    })

还没有被处决。

第一个在 0.5 秒后执行,第二个在传递给 window.subscribe(...) 的函数执行时执行,因为发出的值稍后将在 某个 点。

public social(providerName:string):Observable<Response>{

    let response$ = new Subject<Response>();
    let url =providerName+'/someUrl'

    let authWindow;

    if(window.cordova){
        let authWindow= InAppBrowser.open(url, '_blank', 'location=no');

        authWindow.addEventListener('loadstop', event => {
            if(event.url.indexOf(`${providerName}/callback`) > -1){
                authWindow.close()
            }
        });

        authWindow.addEventListener('loaderror', event =>{
            if(event.url.indexOf(`${providerName}/callback`) > -1){
                authWindow.close()
            }
        });

        authWindow.addEventListener('exit', () => {
            setTimeout(()=>{
                 this.http.get(...)
                 .map(val => val.json())
                 .subscribe(val => response$.next(val);
            },500)
        })

    }else{
        authWindow = createWindow(popupUrl);
        let window = Observable.interval(1000);

        let  pingWindow = window.subscribe(
            ()=>{
                if(authWindow.closed){
                    pingWindow.unsubscribe();

                    this.http.get(...)
                    .map(val => val.json())
                    .subscribe(val => response$.next(val);
                }
            })
    }

    return response$.asObservable()
}

这应该至少可以解决您眼前的问题。