简体   繁体   中英

What does the <var> syntax in Angular / Typescript mean?

In this line:

getHeroes (): Observable<Hero[]> {}

What is the < variable > ?

I've been reading through the docs, and Googling it, but Google seems to think I only want arithmetic operators. Looking for ECMA notation as well, can't find it either.

Or is it a typescript thing?

for this line of code

getHeroes (): Observable<Hero[]> {}

it will return Observable (when you see <T> that is syntax for Generics , so here in this case Observable ie Obersable<Heroe[]> ) which will give you array of Hero class. Observable is part of RxJs .

So if you want to get value you need to subscribe to observable like as below

let heroes:Hero[];
this.getHeroes().subscribe( data=> heroes=data);

I suggest you read concept of Generics and also RxJs , then you will get clear picture.

This line getHeroes (): Observable<Hero[]> {} represents your function, which return an Observable of an array of your Hero class/interface.

To get the value, you have to subscribe to this function and access your data through a passed callback like:

getHeroes().subscribe(data => console.log(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM