Angular 4示例教程类型脚本错误

标签 angular typescript

我正在试用 Angular 网站中给出的教程。我遇到了障碍,无法找出问题所在。

我使用 Angular CLI 创建了这个项目。

app.component.ts

import { Component } from '@angular/core';
import {Hero} from './hero';



const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];


@Component({
  selector: 'app-my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  selectedHero: Hero;
  heroes = HEROES;
  onSelect(hero: Hero): void {
  this.selectedHero = hero;
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import {HeroDetailComponent} from './hero-detail.component';

@NgModule({
  declarations: [
    AppComponent,
    HeroDetailComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

hero-detail.component.ts

import {Component, Input} from '@angular/core';
import {Hero} from './hero';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html'
})
export class HeroDetailComponent {
@Input() hero: Hero;
}

hero.ts

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

源代码结构:

enter image description here

启动应用程序时出现以下 TS 错误

Failed to compile.

/Users/name/Documents/workspaces/angular-tutorial1/firstapp/src/app/hero-detail.component.ts (3,12): Argument of type '{ selector: number; templateUrl: string; }' is not assignable to parameter of type 'Component'. Types of property 'selector' are incompatible. Type 'number' is not assignable to type 'string'.

最佳答案

app-component.html 有这样一行吗?

<app-hero-detail [hero]="selectedHero"></app-hero-detail> 

关于Angular 4示例教程类型脚本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46470104/

相关文章:

reactjs - 使用 Typescript 进行 React defaultProps 对象数组

typescript - Angular 2 导入语句通配符语法

javascript - 什么是更好的方法以及如何实现将对象从一个嵌套组件发送到另一个

javascript - 在 Angular 中将图像 url 转换为 base64

javascript - 为什么 ngIf 不能在 angularjs2 中工作

node.js - 如何让 Typescript 转译我的 Sequelize 模型?

json - '+' 字符在 HttpParams Angular 6 中转换为空格

angular - 同一个变量的输入和输出

javascript - 对象的非空逻辑检查

javascript - JQuery 插件子方法