typescript 怀疑参数没有引用正确的变量

von4xj4u  于 2023-08-08  发布在  TypeScript
关注(0)|答案(1)|浏览(137)

我在试着理解这里发生了什么。我有两个变量activeToInactiveinactiveToActive,每当我更新状态时,我都会递增它们。首先是我的代码
柜台服务:

export class CounterService {
    // Initial States
    activeToInactive = 0
    inactiveToActive = 0
    
    incrementStatusCount(statusCount: number) {
        // Increment either of the initial states whenever the function is called.
        statusCount++
        if (statusCount == this.activeToInactive) {
            console.log(`${statusCount} - active to inactive`)
        }
        if (statusCount == this.inactiveToActive) {
            console.log(`${statusCount} - inactive to aactive`)
        }
        // I notice that statusCount does equal the initial states so if statements evaluate to false
        
    }
}

字符串
接送服务:

import { Injectable } from "@angular/core";
import { CounterService } from "./counter.service";

@Injectable()
export class transferService {

    constructor(private Counter: CounterService) {}
    activeUsers = ['Max', 'Anna'];
    inactiveUsers = ['Chris', 'Manu']  
    
    setToInactive(id: number) {
        this.inactiveUsers.push(this.activeUsers[id]);
        this.activeUsers.splice(id, 1);

        // Calling the function in a different service
        this.Counter.incrementStatusCount(this.Counter.activeToInactive)
    }
      
    setToActive(id: number) {
        this.activeUsers.push(this.inactiveUsers[id]);
        this.inactiveUsers.splice(id, 1);

        // Calling the function in a different service
        this.Counter.incrementStatusCount(this.Counter.inactiveToActive)
    }  
}


调用Transfer Service中的任一函数时,初始状态都不会递增。我所知道的是statusCount不等于任何初始状态。这和推荐信有关吗?我还没有掌握他们,但如果你有任何提示,我可以如何处理这一点,然后让我知道。
提前感谢!!!

eimct9ow

eimct9ow1#

你已经很接近了。
incrementStatusCount函数中,您将接收一个参数statusCount,您打算使用该参数根据传递给函数的值递增activeToInactiveinactiveToActive
但是,在TransferService中,当调用this.Counter.incrementStatusCount(this.Counter.activeToInactive)this.Counter.incrementStatusCount(this.Counter.inactiveToActive)时,传递的是activeToInactiveinactiveToActive的初始值,而不是要递增的实际计数。所以我们直接使用inactiveToActiveactiveToInactive
像这样更新您的CounterService:

incrementStatusCount(incrementActiveToInactive: boolean) {
    // Increment either of the initial states based on the provided flag.
    if (incrementActiveToInactive) {
        this.activeToInactive++;
        console.log(`${this.activeToInactive} - active to inactive`);
    } else {
        this.inactiveToActive++;
        console.log(`${this.inactiveToActive} - inactive to active`);
    }
}

字符串
您的TranserService只会稍微改变,如下所示:

setToInactive(id: number) {
        this.inactiveUsers.push(this.activeUsers[id]);
        this.activeUsers.splice(id, 1);

        // Call the function with the correct flag
        this.Counter.incrementStatusCount(true);
    }

    setToActive(id: number) {
        this.activeUsers.push(this.inactiveUsers[id]);
        this.inactiveUsers.splice(id, 1);

        // Call the function with the correct flag
        this.Counter.incrementStatusCount(false);
    }

相关问题