0

I am new to angular, start adding a new component using ng generate component "name" it works fine but when i add the implements OnInit the compiler start complains and gives

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

this is my code below:

import { Component, Injectable, Input, OnInit } from '@angular/core'; import { User } from './user.model';

@Component({
  selector: 'app-address-card',
  templateUrl: './address-card.component.html',
  styleUrls: ['./address-card.cpmponenet.css']
})
@Injectable()
export class AddressCardComponent implements OnInit{
  user: any;
 @Input('user') userDTO?: User;
  ngOnInit(): void {
    this.user = {
      name: this.userDTO?.name,
      title: this.userDTO?.designation,
      address: this.userDTO?.address,
      phone:this.userDTO?.phone
    };
  }

}

ts version : 4.8.2 angular version: 15.2.4 I have checked stack overflow but nothing helpful... any advice would be much appreciated.

EDIT1: just try to create a new component: the ts file look like this:

@Component({
  selector: 'app-address-card',
  templateUrl: './address-card.component.html',
  styleUrls: ['./address-card.component.css']
})
export class AddressCardComponent implements OnInit{
  ngOnInit(): void {
    throw new Error('Method not implemented.');
  }

}

with an error say under the "AddressCardComponent" says

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

kikicoder
  • 383
  • 3
  • 16

1 Answers1

0

Remove @Injectable(). That is a decorator used for creating classes that can be injected into other classes using Angular's dependency injection system. It is used for services such as services.

@Component however, is a decorator used to create a component. It is used to give Angular information on how to create and use a component. It is used to create components that contain the logic and template for the view of an application that you seem to be doing at the moment.

More explained here: https://stackoverflow.com/a/36494992/15439733

Joosep Parts
  • 5,372
  • 2
  • 8
  • 33