在TypeScript中使用构造函数参数

Has*_*sef 1 typescript

如下图所示,我需要操纵构造函数的输入参数,commandsList但即使使用this.,它也会提供未知变量。

我很伤心,它接受了此参数,并将其passed it作为同一类中另一个方法的输入参数,这迫使我编写一个单独的方法来处理所需的几行内容。

有什么帮助吗?

图片

Sar*_*ana 5

您必须使其成为实例变量。当前,它只是一个参数。您可以像这样从构造函数创建自动实例变量

constructor(private commandslist: ICommandList) {
   console.log(this.commandslist);
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望明确声明它:

private commandslist: ICommandList;

constructor(commandslist: ICommandList) {
    this.commandslist = commandslist;
    console.log(this.commandslist);
}
Run Code Online (Sandbox Code Playgroud)