Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RC5 NgModules declarations

After upgrading angular 2 to RC5, i was getting warnings like below asking me to move my components to module declarations:

NgModule AppModule uses AcademylistComponent via "entryComponents" but it was neither declared nor imported! This warning will become an error after final.

I was refering to these components in my router config file. What looked like this:

import {provideRouter,RouterConfig} from '@angular/router';
import {AcademylistComponent} from '../modules/home/component/academyList.component';
import {CourselistComponent} from '../modules/home/component/courseList.component';
import {CreateacademyComponent} from '../modules/home/component/createAcademy.component';
import {ReportsComponent} from '../modules/home/component/reports.component';
import {AuthenticatedGuard} from '../guards/authenticated.guard';

export const routes: RouterConfig = [
{
    path: '',
    redirectTo:'/home',
    terminal:true},
{
    path: 'home',
    canActivate: [AuthenticatedGuard],
    children: [

        {path: '', component: AcademylistComponent},
        {path: 'my-academies', component: AcademylistComponent},
        {path: 'my-courses', component: CourselistComponent},
        {path: 'create-academy', component: CreateacademyComponent},
        {path: 'reports', component: ReportsComponent}

    ]

}

];

export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];

When i moved the components to ng module's declarations array and imported them there, the routes config file did started giving me Cannot find name errors.

So how do i use module declarations in this case?

like image 617
xmaestro Avatar asked Aug 11 '16 11:08

xmaestro


People also ask

What is NgModules?

NgModules configure the injector and the compiler and help organize related things together. An NgModule is a class marked by the @NgModule decorator. @NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime.

What are declarations in Angular?

Angular Concepts declarations are to make directives (including components and pipes) from the current module available to other directives in the current module. Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported.

How many types of Angular modules are there?

There are two types of modules, root modules and feature modules.


1 Answers

Even if you declare them in your routes, You still have to declare the components used in the routes in the NgModule.

@NgModule({
  declarations: [
    AcademylistComponent,
    //.. and so on
  ], 
  providers: [
    APP_ROUTER_PROVIDERS
  ]
})
export class AppModule {}
like image 135
Ed Morales Avatar answered Oct 31 '22 15:10

Ed Morales