Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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
Javascript 角度2+;从http请求返回变量或映射_Javascript_Angular_Rest_Typescript - Fatal编程技术网

Javascript 角度2+;从http请求返回变量或映射

Javascript 角度2+;从http请求返回变量或映射,javascript,angular,rest,typescript,Javascript,Angular,Rest,Typescript,我有一个服务,它要么存储一个对象,以便我可以在其他组件中访问它,要么从web api请求数据并在映射中返回它 objectService的示例get get(): any { if (this.object == undefined || this.object == null) { return this.http.get(this.baseUrl + "/companies/get) .map((data: any) => {

我有一个服务,它要么存储一个对象,以便我可以在其他组件中访问它,要么从web api请求数据并在映射中返回它

objectService的示例get

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return this.object;
    }
}
ngOnInit() {
    this.objectService.get().subscribe((data: any) => {
        this.title.setTitle(data.name);
    });
}
get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
            });
    }
    return this.object;
}
ngOnInit() {
    this.title.setTitle(this.objectService.get().name);
}
使用服务设置页面标题

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return this.object;
    }
}
ngOnInit() {
    this.objectService.get().subscribe((data: any) => {
        this.title.setTitle(data.name);
    });
}
get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
            });
    }
    return this.object;
}
ngOnInit() {
    this.title.setTitle(this.objectService.get().name);
}
因此,当我第一次加载页面时,这是可行的,但是在没有重新加载的情况下导航到页面时,我得到以下错误
…subscribe不是一个函数
。这很有意义,因为我返回的是一个对象,而不是http请求

所以我尝试只返回对象,而不是http请求

objectService的示例get

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return this.object;
    }
}
ngOnInit() {
    this.objectService.get().subscribe((data: any) => {
        this.title.setTitle(data.name);
    });
}
get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
            });
    }
    return this.object;
}
ngOnInit() {
    this.title.setTitle(this.objectService.get().name);
}
使用服务设置页面标题

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return this.object;
    }
}
ngOnInit() {
    this.objectService.get().subscribe((data: any) => {
        this.title.setTitle(data.name);
    });
}
get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
            });
    }
    return this.object;
}
ngOnInit() {
    this.title.setTitle(this.objectService.get().name);
}
现在,这扭转了页面刷新给我一个错误的问题,即说.objectService.get().name未定义,当我导航到我的组件而不进行页面刷新时,它工作正常


如果您能帮助我们实现这一目标,我们将不胜感激。提前谢谢

实际上,你是在混合可观察到的和正常的物体

我会说,坚持第一个aproach,但在只返回对象的同时,将其转换为可观察对象,以便它在您的
onNgInit

所以你的服务会

get(): Observable<any> {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return Observable.of(this.object);
    }
}

通过
observable.of('this.object')
将普通对象转换为可观察对象,假设http请求返回404。我如何在ngOnInit函数中检测到这一点?Nvm解决了这个问题,我在服务中的
映射之后设置了一个
catch
,我可以返回错误并在使用服务时检查它。您也可以在组件端通过传递第二个函数进行订阅。