如何正确使用全局变量?

huangapple go评论153阅读模式
英文:

How Do I Use Global Vars properly?

问题

我有一个分页脚本,我在增加当前页码方面遇到了困难,以便通过AJAX调用下一页...

在我的初始TypeScript文件中,我这样声明全局VAR:

declare var getCurrentPage: number;

然后在同一个文件中,我从初始的AJAX调用中为这个值定义了如下内容:

globalThis.getCurrentPage = data && data.startPage;
currentPage: getCurrentPage || 0,

之所以这样定义是因为currentPage也可能是另一个值,或者在getCurrentPage失败或不存在时设置为零。

现在,在我的分页文件中,与上述文件分开(这是因为初始值是从AJAX请求中获取的),我像这样递增我的值,然后将它们发送回全局VAR:

const nextPage = this.currentPage + 1;
globalThis.getCurrentPage = nextPage;

然而,这给了我一个nextPage值为1,一个currentPage值为0,每次我按下“下一页”按钮时,它都会给我一个nextPage值为1和一个currentPage值为0,因为0 + 1等于1...

我做错了什么?

英文:

strong textI have a pagination script and I am struggling to increment the current page so I can call the next page via AJAX...

In my initial typescript file I am declaring the global VAR like this;

declare var getCurrentPage: number;

then later on in the same file I am defining the value for this from my initial AJAX call

globalThis.getCurrentPage = data && data.startPage;
currentPage: getCurrentPage || 0,

It is defined this way because the currentPage could also be another value, or is set to zero if getCurrentPage fails or doesn't exist.

Now in my pagination file, which is separate to the above (this is because the initial values are derived from the AJAX request), I increment my values like this, then send them back to the global VAR

const nextPage = this.currentPage + 1;
globalThis.getCurrentPage = nextPage;

This however is giving me a nextPage value of 1, and a currentPage value of 0, and every time I press my Next Page button, it keeps giving me a nextPage value of 1 and a currentPage value of 0, because 0 + 1 is 1...

What am I doing wrong?

答案1

得分: 1

似乎在处理全局变量 getCurrentPage 方面存在误解。根据您的描述,似乎在开始时将变量 currentPage 设置为 getCurrentPage 的值。然后,当您增加 currentPage 时,将增加后的值赋给 getCurrentPage,这不是您想要的行为。

让我澄清您需要采取的步骤:

  1. 首先,请确保全局变量 getCurrentPage 已经在初始的 TypeScript 文件和分页文件中定义并且可以访问。

  2. 在初始的 TypeScript 文件中,在接收到初始 AJAX 调用的数据后,根据从 AJAX 响应中接收到的值设置 currentPage 变量:

    declare var getCurrentPage: number;
    
    // 在您的 AJAX 调用之后
    globalThis.getCurrentPage = data && data.startPage;
    const currentPage = getCurrentPage || 0;
  1. 在分页文件中,增加 currentPage 的值,然后更新全局变量 getCurrentPage 为新的值:
    // 在分页文件中
    const nextPage = currentPage + 1;
    globalThis.getCurrentPage = nextPage;

通过这样做,您将正确地增加 currentPage 的值,并将全局变量 getCurrentPage 更新为最新的值。下次点击“下一页”按钮时,它应该正确地增加值。

请确保检查您的代码中可能影响行为的任何其他部分,并确保您没有在代码的其他地方意外重新初始化 currentPage 变量。此外,请记住,使用全局变量应谨慎,因为它们可能导致意外的行为,并使代码更难以维护。考虑使用适当的封装和作用域技术来更有效地管理状态。

英文:

It seems there is a misunderstanding in how you are handling the global variable getCurrentPage. Based on your description, it looks like the variable currentPage is being set to the value of getCurrentPage at the beginning. Then, when you increment currentPage, you are assigning the incremented value to getCurrentPage, which is not what you want.

Let me clarify the steps you need to take:

  1. First, ensure that the global variable getCurrentPage is defined and accessible from both the initial TypeScript file and the pagination file.

  2. In the initial TypeScript file, after receiving the data from the initial AJAX call, set the currentPage variable based on the value received from the AJAX response:


    declare var getCurrentPage: number;
    
    // After your AJAX call
    globalThis.getCurrentPage = data && data.startPage;
    const currentPage = getCurrentPage || 0;

  1. In the pagination file, increment the currentPage value and then update the global variable getCurrentPage with the new value:

    // In the pagination file
    const nextPage = currentPage + 1;
    globalThis.getCurrentPage = nextPage;

By doing this, you will increment the currentPage value correctly and update the global variable getCurrentPage with the latest value. The next time you click the Next Page button, it should increment the value correctly.

Make sure to check for any other parts of your code that might be affecting the behavior, and ensure that you are not accidentally reinitializing the currentPage variable somewhere else in your code. Also, remember that using global variables should be done with caution as they can lead to unexpected behavior and make the code harder to maintain. Consider using proper encapsulation and scoping techniques to manage state more effectively.

huangapple
  • 本文由 发表于 2023年8月5日 16:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定