为什么这会调用javascript

时间:2018-02-15 14:10:50

标签: javascript angular5

我创建一个angular5应用程序,我用来向简单节点后端服务器发出请求的服务有一些问题。我不会包含后端代码,因为无论它是否有效都无关紧要。

我的想法是让服务记住,如果它已经有数据,如果有,请不要再打电话。我创建了一个名为useCache的布尔值来跟踪是否进行另一次调用。代码如下所示。

getPeople() {
    console.log('before ', this.useCache);
    if (this.useCache === false) {
      console.log('after', this.useCache);
      this.setPeople(this.api.get(this.url));
      this.useCache = true;
    }
    return this.people;
}

useCache初始化为false,因此第一次应该进行调用。 第二次+次打印useCache为真。 当按下多次触发GET调用的按钮时,此代码不会记录('after',this.useCache)。但是我的后端服务器仍在注册呼叫。 people是一个未定义的变量,最初是type any

我也尝试登录api.get函数,也不打印该日志。

public get(url: string) {
    console.log('get');
    return this.http.get(API_BASE_URL + url).map(response => {
        return response;
    }).catch(this.handleError);
}

我从i调用的组件内部有这个函数来调用getPeople()函数。

getPeople() {
    this.personService.getPeople().subscribe(result => {
        console.log(result);
        this.people = result.result;
        this.dataType = 'people';
    });
}

有没有人知道我的后端如何/为什么还在记录GET调用? 它应该不知道我在单独的前端应用程序中按下了那个按钮吗?

0 个答案:

没有答案