Angular typescript 传递服务函数

Angular typescript pass service function

我的服务中有两个方法做的事情非常相似,所以订阅是一样的。我想加入执行程序,所以在启动我的服务方法时

this.createNewUser(this.userModel, this.userDefaultGroupSelectedId)
            .subscribe(
            res => {
                this.msgs = ({ severity: "success", summary: "Success:\n", detail: res });
                this.userModel = new User();
                this.onConfirm.emit();
            },
            error => {
                this.msgs = ({ severity: "error", summary: "Error Message:", detail: error.Message ? error.Message : error.toString() });
            }

它会调用 createNewUser shold return 应该调用哪个函数

createNewUser(user: IUser, userDefaultGroupSelectedId: number): Observable<string> 
    {
        if (this.asRegistration == false) {
            return this.userAdministrationService.addNewUser(this.userModel, this.userDefaultGroupSelectedId);
        }
        else {
            this.userAdministrationService.registerNewUser(this.userModel);
        }
    }

但是这个方法不行,我得到了Cannot read property 'subscribe' of undefined

我的服务:

addNewUser(user: IUser, userDefaultGroupSelectedId: number): Observable<string> {

    return this.http.post(this.addNewUserUrl)
        .catch(error => super.handleError(error))
        .map(response => response.json());
}

registerNewUser(user: IUser): Observable<string> 
{

    return this.http.post(this.registerNewUserUrl)
        .catch(error => super.handleError(error))
        .map(response => response.json());
}

问题好像是你在this.userAdministrationService.registerNewUser(this.userModel);的结果中不是return,所以本例函数的return是undefined

尝试将其更改为:

return this.userAdministrationService.registerNewUser(this.userModel);