1

I am trying to create simple router based application in Angular2 using typescript. My Angular2 version is 2.0.0-rc.4 and router version is 3.0.0-beta.1

My Routes configuration follows-

App.routes.ts

import { provideRouter, RouterConfig } from '@angular/router';
import { PageNotFoundComponent } from './Shared/pageNotFound.component';
import { Page1Component } from './Pages/Page1/page1.component';
import { Page2Component } from './Pages/Page2/page2.component';

const routes: RouterConfig = [

  { path: 'page1', component: Page1Component },
  { path: 'page2', component: Page2Component },
  { path: '**', component: PageNotFoundComponent }
];

export const appRouterProviders = [
  provideRouter(routes)
];

While running application using npm start command I receives one warning (not error). Please refer this screen shot for warning

How do I overcome this warning?

2 Answers 2

0

Try adding below line in your main AppComponent-

  precompile: [Page1Component, Page2Component .... ]

See if this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! adding precompile line goes away warning but on first loading of application I always see PageNotFound component. Am I missing any configuration?
You can set default route like this- { path: '', redirectTo: '/page1', pathMatch: 'full' } This will load your page1 by default.
0

This appears to be a gap in the Angular 2 router documentation. Hopefully it will catch up soon. In the mean time, as described in this answer, the latest release of Angular 2 adds a precompile option to the @Component decorator. As per the warning's request, add this to your app component.

@Component({
  selector: 'my-root-app-component',
  template: '...',
  precompile: [PageNotFoundComponent, PlusWhateverElseThrowsAWarning, Etc]
})

Angular 2 has always precompiled components (it's what allows it to be so magical!) but with the new router it's required that you specify which components should be precompiled. Presumably this is being done to optimize performance ensuring that only components that are absolutely necessary are precompiled on startup.

1 Comment

Thanks! adding precompile line goes away warning but on first loading of application I always see PageNotFound component. Am I missing any configuration?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.