使用' ='之间的区别和':'在typescript中声明类成员时

时间:2018-05-12 11:04:32

标签: typescript

对不起,这可能听起来很无聊。我正在学习Angular,并在编写教程时,我正在尝试找出':'的差异和使用场景。和' ='声明财产时

import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';

@Component({

  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

  heroes= HEROES;

  selectedHero: Hero;

  constructor() { }

  ngOnInit() {
  }

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

1 个答案:

答案 0 :(得分:1)

selectedHero: Hero声明的变量类型为Hero,但变量为undefined

heroes = HEROES声明一个类型为any的变量,并将其分配给值HEROES

TypeScript的完整作业将是 varName: varType = varValue