Home > OS >  Need help in defining and calling(using) Static variable in ts, it is always undefined
Need help in defining and calling(using) Static variable in ts, it is always undefined

Time:07-27

export class myClass implements OnInit {

counter=0;
static counter:any;

onListItemClick(PackDef: PackDefinition): void {
this.itemClicked.emit(PackDef);
this.counter  ;
console.log(this.counter);

}

and..

import { myClass } from '../../../';
dialogRef.afterClosed().subscribe((result: any) => {
this.saveMyMethod();
console.log("Final1"),
console.log(myClass.counter);
});

saveMyMethod(): void {
console.log("Final2"),
console.log(myClass.counter);

} }

Do you know why I cant have correct counter after "Final1" or "Final2"? but I have a correct counter in myClass.

CodePudding user response:

Are you write code with js? js are not support type any and your define variable is use for ts. Try again with follow code below:

static counter = 0;

CodePudding user response:

Try again with follow code below:

export class myClass implements OnInit {
static counter:any = 0;
static onListItemClick(PackDef: PackDefinition): void {
this.counter  ;
console.log(this.counter);
}
  • Related