18

I'm using Angular 2 CLI and I created the component "MyComponent" with the ng generate component MyComponent. As far as I know I have to add the component to the directive key-value-pair of the @Component decorator, but the typescript compilation fails at this point, saying that:

ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.ts:8:2 
Argument of type '{ selector: string; template: any; styles: any[]; directives: typeof MyComponentComponent[]; }' is not assignable to parameter of type 'Component'.
  Object literal may only specify known properties, and 'directives' does not exist in type 'Component'.

This is my code for the app:

import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component/my-component.component'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  directives: [MyComponentComponent],
})
export class AppComponent {
  title = 'app works!';
}

I didn't touch the code of the generated component, but just in case:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})

export class MyComponentComponent implements OnInit {

  constructor() {

  }

  ngOnInit() {
  }

}
Philip Feldmann
  • 7,887
  • 6
  • 41
  • 65

5 Answers5

34

Angular2 Component decorator no longer use these for embedding other components. We will need to use a new meta data called entryComponents instead. See below as an example ...

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  entryComponents:[VehicleListComponent]
})

The vehicle-list-component has the following component metadata..

@Component({
  selector: 'app-vehicle-list',
  templateUrl: './vehicle-list.component.html',
  styleUrls: ['./vehicle-list.component.css'],
  providers: [VehicleService]
})
user6878305
  • 341
  • 2
  • 2
  • 2
    I just replaced "directives" with "entryComponents" and got it to work. Read more here: https://angular.io/docs/ts/latest/api/core/index/Component-decorator.html – wojjas Oct 05 '16 at 21:43
  • 1
    Same issue here, this answer is helpful. Can someone explain why the name change? – Adrian Moisa Oct 28 '16 at 12:56
  • I also make reference on @@NgModule putting the component on declarations key and import it on the top of the file (in my case @@NgModule is on app.module.ts). – Ricardo França Dec 02 '16 at 14:07
18

Error itself says that directives doesn't exist in Component as it has been deprecated. try this code shown below,

import { MyComponentComponent } from './my-component/my-component.component'
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';

@NgModule({
   ...
   ...
   declarations:[AppComponent,MyComponentComponent], //<---need to declare 
   schemas:     [CUSTOM_ELEMENTS_SCHEMA]             //<---added this line
})

And remove directives:[MyComponentComponent] from AppComponent.

micronyks
  • 54,797
  • 15
  • 112
  • 146
1

if you use Angular CLI then the new component will be automatically imported and added to declarations section of @ngmodule in app.module.ts . We don't need to write any code for that.

1

NO NEED TO DO ANYTHING WITH ANGULAR 4 or greater after running the create component command. The error is most likely just you not using the right selector name from the new component metadata file (eg componentname.component.ts).

Jujhar Singh
  • 129
  • 6
0

You have two options:

1: Use entryComponents tag inside Component

2: Use Angular CLI which automatically imports it, so we don't need to explicitly write code for it.