首页 > 解决方案 > 如何从“app/Athlete”导入{ Athlete }?

问题描述

我卡住了。我正在从其中一本书中做示例 Angular 应用程序,代码中有时会出现错误。我找不到其中之一的解决方案。

运行应用程序后,我收到一个错误:

src/app/app.component.ts:2:25 中的错误 - 错误 TS2307:找不到模块“app/Athlete”或其相应的类型声明。

2 从“应用程序/运动员”导入{运动员};

我的代码如下。tsconfig.json

{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    },
]
}

app.component.ts

import { Component } from '@angular/core';
import { Athlete } from "app/Athlete";

@Component({
  selector: 'app-root',
  template: `<h1>Pięciu najlepszych zawodników w Kona</h1>
  <app-athlete-list (selected)=showDetails($event)>Wczytywanie listy zawodników...</app-athlete-list>
  Wybrałeś: {{selectedAthlete}}`
})
export class AppComponent {
  selectedAthlete: string;

  constructor (){
    this.selectedAthlete="żaden";
  }

  showDetails(selectedAthlete: Athlete) {
    this.selectedAthlete=selectedAthlete.name;
  }
}

app.module.ts

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

import { AppComponent } from './app.component';
import { AthleteService } from './athlete.service';
import { AthleteListComponent } from './athlete-list.component';
import { AthleteComponent } from './athlete.component';

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

运动员.ts

export class Athlete {
  name: string;
  country: string;
  time: string;
}

注意:请注意,它athlete.tsapp.component.ts.

我已经尝试过这个修复,但没有奏效。并且找不到其他任何东西。我会感谢你的帮助。

标签: angular

解决方案


将其更改为:

import { Athlete } from 'src/app/Athlete';

推荐阅读