Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class is using Angular features but is not decorated. Please add an explicit Angular decorator

I have some components like CricketComponent, FootballComponent, TennisComponent etc. All These Classes have some common properties :- TeamName, teamSize, players etc which are @Input().

Now I created a BaseComponent class, defined all these properties in there and this baseComponent class will be extended by cricket/football/tennis/etcComponents.

baseComponent.ts

export class BaseComponent {      @Input() TeamName: string;     @Input() teamSize: number;     @Input() players: any;  } 

CricketComponent.ts

@Component({   selector: 'app-cricket',   templateUrl: './cricket.component.html',   styleUrls: ['./cricket.component.scss'] }) export class cricketComponent extends BaseComponent implements OnInit {    constructor() {     super();   }    ngOnInit(): void {   }  } 

I am getting this error:

ERROR in src/app/base-screen.ts:4:14 - error NG2007:

Class is using Angular features but is not decorated. Please add an explicit Angular decorator.

like image 602
abhay tripathi Avatar asked Jul 28 '20 02:07

abhay tripathi


People also ask

Can we use Angular features but not decorated?

answer re: Class is using Angular features but is not decorated. Please add an explicit Angular decorator. You'll need to add a @Component decorator to that base class (which should probably also be declared abstract ).

What is the decorator to declare a class as directive in Angular?

Directivelink. Decorator that marks a class as an Angular directive. You can define your own directives to attach custom behavior to elements in the DOM.

What is Angular decorator?

Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage.

Can we create custom decorator in Angular?

Decorators are a new feature of TypeScript and used throughout the Angular code, but they are nothing to be scared of. With decorators we can configure and customise our classes at design time. They are just functions that can be used to add meta-data, properties or functions to the thing they are attached to.


1 Answers

You'll need to add a @Component decorator to that base class (which should probably also be declared abstract).

This is the bare minimum you can get away with in Angular 9:

@Component({   template: '' }) export abstract class BaseComponent {      @Input() teamName: string;     @Input() teamSize: number;     @Input() players: any; } 

For Angular 10+, see this answer.

like image 60
Robby Cornelissen Avatar answered Oct 26 '22 15:10

Robby Cornelissen