#angular #typescript #atom-editor
Вопрос:
Для университета мне нужно написать проект на английском языке . Проект, который нам нужно написать, это: -Создать класс автомобилей-Создать класс спортивных автомобилей, который наследует класс автомобилей. — Создайте гоночный класс для класса спортивных автомобилей. — Покажите два спортивных автомобиля с тремя гонками каждый.
Я пишу все классы, но в командной строке у меня есть эта ошибка : «Класс» CarComponent «указан в объявлениях NgModule «AppModule», но не является директивой, компонентом или каналом. Либо удалите его из объявлений NgModule, либо добавьте соответствующий угловой декоратор».
Это мой код машинописного текста :
`import { Component } from '@angular/core';
@Component({
selector:'app-car',
templateUrl:'./car.component.html'
})
public class Car {
name:string;
model:string;
price:number;
year:number;
constructor(name:string,model:string,price:number,year:number){
this.name=name;
this.model=model;
this.price=price;
this.year=year;
}
}
class SportCar extends Car{
private raceNumber:number;
private Races:race[];
public getAllRaces() {
let temp:race[];
for (let index = 0; index < this.Races.length; index ) {
let tempRace= new race(this.Races[index].location,this.Races[index].date,this.Races[index].line,this.Races[index].placeInRace,this.Races[index].isWinner);
temp.push(tempRace);
}
return temp;
}
public addNewRace(location:string,date:string,line:string,placeInRace:number,isWinner?:boolean) {
if(!isWinner){
let newRace:race = new race(location,date,line,placeInRace);
this.Races.push(newRace);
}
else{
let newRace:race = new race(location,date,line,placeInRace,isWinner);
this.Races.push(newRace);
}
}
}
class race {
constructor(public location:string,public date:string,public line:string,public placeInRace:number, public isWinner?:boolean) {
}
}
export class CarComponent {
let car1 = new Car('Ferrari', '488 Spider', 500000, 2007);
let car2= new Car('McLaren','F1 GTR',5300000,1995);
//I need to write their races
}`
и это код модуля app.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CarComponent } from './car/car.component';
@NgModule({
declarations: [
AppComponent,
CarComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Спасибо
Ответ №1:
Сначала вам нужно разместить декоратор «@Компонент » прямо над объявлением класса CarComponent, например :
@Component({
selector:'app-car',
templateUrl:'./car.component.html'
})
export class CarComponent {
}
Затем вам нужно узнать больше о классах в TypeScript :
https://www.typescriptlang.org/docs/handbook/classes.html