Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 如何缓存和共享Http get()响应?_Angular_Rxjs5_Angular2 Http - Fatal编程技术网

Angular 如何缓存和共享Http get()响应?

Angular 如何缓存和共享Http get()响应?,angular,rxjs5,angular2-http,Angular,Rxjs5,Angular2 Http,遵循本课程和该课程中的github材料,尝试 避免每次单击链接时调用http.get()请求。 我认为每次加载文件而不是将其作为对象保存在内存中是一种巨大的浪费 正在尝试替换此代码: getProducts(): Observable<IProduct[]> { return this._http.get(this._productUrl) .map((response: Response) => <IProduct[]> respo

遵循本课程和该课程中的github材料,尝试 避免每次单击链接时调用http.get()请求。 我认为每次加载文件而不是将其作为对象保存在内存中是一种巨大的浪费

正在尝试替换此代码:

    getProducts(): Observable<IProduct[]> {
    return this._http.get(this._productUrl)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
}

最后一行不应该出现

\u之前可观察到的是日志中的对象。“最后一行”注释在if-else之外。为什么不试试:

    if (!Object.keys(this._observable).length) {
   console.log('_observable inside 1: ' + (this._observable));
    this._observable=this._http.get(this._productUrl)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All inside observable: ' +  JSON.stringify(data)))
        .catch(this.handleError);
        console.log('_observable inside 2: ' + (this._observable));
        return this._observable;
   } else {
        console.log('_observable after: ' + (this._observable));
        return this._observable;
   }
if(!Object.keys(this.\u observable).length){
console.log(“'u observable in 1:'+(this.'u observable));
this.\u observable=this.\u http.get(this.\u productUrl)
.map((response:response)=>response.json())
.do(data=>console.log('All-inside-observable:'+JSON.stringify(data)))
.接住(这个.把手错误);
console.log(“'u observable in 2:'+(this.'u observable));
返回此项。\u可观察到;
}否则{
console.log(“'u observable after:'+(this.'u observable));
返回此项。\u可观察到;
}

\u之前可观察到的是日志中的对象。“最后一行”注释在if-else之外。为什么不试试:

    if (!Object.keys(this._observable).length) {
   console.log('_observable inside 1: ' + (this._observable));
    this._observable=this._http.get(this._productUrl)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All inside observable: ' +  JSON.stringify(data)))
        .catch(this.handleError);
        console.log('_observable inside 2: ' + (this._observable));
        return this._observable;
   } else {
        console.log('_observable after: ' + (this._observable));
        return this._observable;
   }
if(!Object.keys(this.\u observable).length){
console.log(“'u observable in 1:'+(this.'u observable));
this.\u observable=this.\u http.get(this.\u productUrl)
.map((response:response)=>response.json())
.do(data=>console.log('All-inside-observable:'+JSON.stringify(data)))
.接住(这个.把手错误);
console.log(“'u observable in 2:'+(this.'u observable));
返回此项。\u可观察到;
}否则{
console.log(“'u observable after:'+(this.'u observable));
返回此项。\u可观察到;
}
查看您的代码

 getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
    .map((response: Response) => <IProduct[]> response.json())
    .publishReplay(1)
     .refCount()
    .do(data => console.log('All: ' +  JSON.stringify(data)))
    .catch(this.handleError);
}
getProducts():可观察{
返回此。\ http.get(此。\产品URL)
.map((response:response)=>response.json())
.publishReplay(1)
.refCount()
.do(data=>console.log('All:'+JSON.stringify(data)))
.接住(这个.把手错误);
}
现在你不需要考虑这些条件。publishReplay、refCount将在所有观察者之间共享相同的状态。因此publishReplay将帮助您缓存数据,而refCount将帮助observer可用。

了解您的代码

 getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
    .map((response: Response) => <IProduct[]> response.json())
    .publishReplay(1)
     .refCount()
    .do(data => console.log('All: ' +  JSON.stringify(data)))
    .catch(this.handleError);
}
getProducts():可观察{
返回此。\ http.get(此。\产品URL)
.map((response:response)=>response.json())
.publishReplay(1)
.refCount()
.do(data=>console.log('All:'+JSON.stringify(data)))
.接住(这个.把手错误);
}

现在你不需要考虑这些条件。publishReplay、refCount将在所有观察者之间共享相同的状态。因此publishReplay将帮助您缓存数据,而refCount将帮助observer可用。

为了避免加载文件,您需要在if语句中包含代码行:

            .publishReplay(1)
            .refCount()
完整代码如下:

    getProducts(): Observable<IProduct[]> {
    console.log('_observable before: ' + (this._observable));
    if(this._observable===undefined){
        console.log('_observable inside 1: ' + (this._observable));
        this._observable=this._http.get(this._productUrl)
            .map((response: Response) => <IProduct[]> response.json())
            .publishReplay(1)
            .refCount()
            .do(data => console.log('All inside observable: ' +  JSON.stringify(data)))
            .catch(this.handleError);
        console.log('_observable inside 2: ' + (this._observable));
    }

    console.log('_observable after: ' + (this._observable));
    return this._observable;
}
getProducts():可观察{
console.log(“'u observable before:'+(this.'u observable));
if(本可观察===未定义){
console.log(“'u observable in 1:'+(this.'u observable));
this.\u observable=this.\u http.get(this.\u productUrl)
.map((response:response)=>response.json())
.publishReplay(1)
.refCount()
.do(data=>console.log('All-inside-observable:'+JSON.stringify(data)))
.接住(这个.把手错误);
console.log(“'u observable in 2:'+(this.'u observable));
}
console.log(“'u observable after:'+(this.'u observable));
返回此项。\u可观察到;
}

为了避免加载文件,您需要在if语句中包含To行代码:

            .publishReplay(1)
            .refCount()
完整代码如下:

    getProducts(): Observable<IProduct[]> {
    console.log('_observable before: ' + (this._observable));
    if(this._observable===undefined){
        console.log('_observable inside 1: ' + (this._observable));
        this._observable=this._http.get(this._productUrl)
            .map((response: Response) => <IProduct[]> response.json())
            .publishReplay(1)
            .refCount()
            .do(data => console.log('All inside observable: ' +  JSON.stringify(data)))
            .catch(this.handleError);
        console.log('_observable inside 2: ' + (this._observable));
    }

    console.log('_observable after: ' + (this._observable));
    return this._observable;
}
getProducts():可观察{
console.log(“'u observable before:'+(this.'u observable));
if(本可观察===未定义){
console.log(“'u observable in 1:'+(this.'u observable));
this.\u observable=this.\u http.get(this.\u productUrl)
.map((response:response)=>response.json())
.publishReplay(1)
.refCount()
.do(data=>console.log('All-inside-observable:'+JSON.stringify(data)))
.接住(这个.把手错误);
console.log(“'u observable in 2:'+(this.'u observable));
}
console.log(“'u observable after:'+(this.'u observable));
返回此项。\u可观察到;
}

ProductListComponent是否再次订阅了可观察的组件?如果是这样的话,它会再次执行。看看这里提供的解决方案:实际上,你引导我到了正确的方向,这是关于缓存和添加神奇的单词publishReplay(1)和refCount如果你发布答案,我会将其标记为answer@DeborahK谢谢你的精彩课程!ProductListComponent是否再次订阅可观察的?如果是这样的话,它会再次执行。看看这里提供的解决方案:实际上,你引导我到了正确的方向,这是关于缓存和添加神奇的单词publishReplay(1)和refCount如果你发布答案,我会将其标记为answer@DeborahK谢谢你的精彩课程!结果是相同的结果是sameI希望@DeborahK发布这个已经完成的答案,但是你是第一个没有if语句的人,它没有影响我希望@DeborahK发布这个已经完成的答案,但是你是第一个没有if语句的人,它没有影响