输入'订阅'不能指定类型' Observable'从订阅返回时出错

时间:2017-09-06 05:50:09

标签: typescript rxjs

    setPrivileges(privilege: string): Observable<boolean> {
        if (this.loginService.privilegeList != null && this.loginService.privilegeList != undefined && this.loginService.privilegeList.getValue() != null) {
            this.privileges = this.loginService.privilegeList.getValue().filter(item => item.name === privilege);
            console.log("if this.privileges : "+this.privileges);
                return Observable.of(true);
        } else {
              return this.getUserRolesAndMenus().subscribe((result: any) => {  // line 7
               console.log("result  :"+JSON.stringify(result));
               this.privileges = result.roles.filter((element: any) => {
                    element.privileges.filter((item: any) => {
                        item.name === privilege;
                    })
                })
                console.log("esle this.privileges : "+this.privileges);
                if(this.privileges == null)
                    return Observable.of(true);
                else
                       return Observable.of(true);
            });
        }
    }

在第7行,它会抛出异常

[TS]

键入&#39;订阅&#39;不能分配给&#39; Observable&#39;。   物业&#39; _isScalar&#39;在订阅&#39;类型中缺少。 (方法)UserService.getUserRolesAndMenus():Observable 获取用户详细信息。 &#39;

这里做错了什么?

/**
     * To get the user details.
     */
    getUserRolesAndMenus(): Observable<any> {
        let headers = new Headers();
        headers.append(AppUtils.HEADER_AUTHENTICATION, localStorage.getItem(AppUtils.STORAGE_ACCOUNT_TOKEN));
        return this.http.get(AppUtils.GET_USER_ROLES_AND_MENUS + AppUtils.SYSTEM_ID_IRS, { headers: headers })
            .map(response => response.json())
            .catch(this.handleError)
           /* .publishReplay(1)
            .refCount();*/
    }

1 个答案:

答案 0 :(得分:2)

您正在返回订阅而不是Observable。将subscribe更改为switchMap。这将返回一个Observable并用getUserRolesAndMenus

创建的Observable替换Observable.of(...)的结果