Angular2 Hero Tour. What does the colon in onSelect (hero: hero) mean?

I am doing an Angular2 tour of the heroes project https://angular.io/docs/ts/latest/tutorial/toh-pt2.html .

<li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}</li> 

Here I can report the current name and identifier of the hero using the following function

onSelect(hero) {
        alert(hero.id);
   }

But why in the official lesson does he use

onSelect(hero: Hero){

    }

why hero: hero ?

as well as what it means onSelect(hero: Hero): void { }.

What is the meaning

selectedHero: Heroes;
      onSelect(hero: Heroes): void {
      this.selectedHero = hero;
    }

Please help.

+8
source share
6 answers

"", , "any". : , "", , Hero .

EDIT: void . , .

EDIT2:

selectedHero: Hero

OnSelect(hero: Hero): void{
    this.selectedHero = hero;
}

"selectedHero: Hero" "selectedHero" "Hero".

"OnSelect", "" "". "hero" , "OnSelect".

void, , , .

this.selectedHero = ; . Hero "hero". . 'this' , , , . , , , 'this'.

, , OnSelect , , (: "" ). , , (this.selectedHero) , (: ).

, , Hero , .

PS: 'this' , , , , , , , ,

+8

:

methodName(variable: Class): returnType {
    // your code here
}

: .

+4

, "".

,

export class Hero {
    id: number;
    name: string;
}


onSelect(hero: Hero){}
+2

TypeScript , String, Number, Boolean, any, .

JavaScript, TypeScript, .

, , .

, class interface.

Hero:

class Hero {
    id: number;
    name: string;
}

:

onSelect(hero: Hero): void {

}

, Hero, .

void , . , void. :

onSelect(hero: Hero): boolean {
    return false;
}

TypeScript , , , , JavaScript.

TypeScript , TypeScript.

selectedHero: Heroes;
   onSelect(hero: Heroes): void {
   this.selectedHero = hero;
}

selectedItem , , , , .

selectedHero. , selectedHero. selectedHero Angular.

+2

hero: Hero

"", , Hero . ,

+1

I thought the 'hero' variable was somehow related to the variables defined in the earlier stages of the tutorial (which were also called 'hero'). But, finally, I realized that this is NOT.

Therefore, I would recommend trying:

selectedHero: Heroes;

      onSelect(whyyoucalleditheroandblurredthewholetutorial: Heroes): void {
      this.selectedHero = whyyoucalleditheroandblurredthewholetutorial;
    }
0
source

Source: https://habr.com/ru/post/1670931/


All Articles