Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 router Error: Invalid configuration of route 'undefined'

I'm using the Angular 2 router 3.0.0-beta.2.

I cannot seem to get a single route to work, I have this error:

"Error: Invalid configuration of route 'undefined': component, redirectTo, children must be provided"

main.ts

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment, appRouterProviders } from './app';

bootstrap(AppComponent, [appRouterProviders])
  .catch(err => console.error(err));

app.routes.ts

import {provideRouter, RouterConfig}  from '@angular/router';
import {HomeComponent} from './';

export const appRoutes:RouterConfig = [
  [{
    path: '',
    redirectTo: '/home',
    pathMatch: 'full'
  },{
     path: 'home',
     component: HomeComponent
  }]
];

export const routes: RouterConfig = [
  ...appRoutes
];

export const appRouterProviders = [
  provideRouter(routes)
];

app.component.ts

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES }  from '@angular/router';

 @Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  directives: [ROUTER_DIRECTIVES]
})
export class AppComponent {
  title = 'app works!';
}

home.component.ts

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

@Component({
  moduleId: module.id,
  selector: 'app-home',
  templateUrl: 'home.component.html',
  directives: [ROUTER_DIRECTIVES]
})
export class HomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }
}

app.component.html

<h1>
  App Shell
</h1>
<router-outlet></router-outlet>
like image 660
JBeckton Avatar asked Jul 26 '16 17:07

JBeckton


3 Answers

For future readers:

This error will also show if the Component in question is not registered on the module, and can be quite confusing.

like image 146
Troels Larsen Avatar answered Nov 19 '22 12:11

Troels Larsen


You need to provide the correct relative path for the HomeComponent import:

instead of this:

import {HomeComponent} from './';

do this:

import {HomeComponent} from './home.component';

app.routes.ts

import {provideRouter, RouterConfig}  from '@angular/router';
import {HomeComponent} from './home.component';  // you need to provide correct relative path

const appRoutes:RouterConfig = [                 //removed export
      {                                          // removed square bracket
        path: '',
        redirectTo: '/home',
        pathMatch: 'full'
      },{
         path: 'home',
         component: HomeComponent
      }
    ];


    export const appRouterProviders = [
      provideRouter(routes)
    ];

main.ts

import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent} from './app.component';     //please provide right path
import {appRouterProviders } from './app.routes';  // added 

bootstrap(AppComponent, [appRouterProviders])
  .catch(err => console.error(err));
like image 36
Nikhil Shah Avatar answered Nov 19 '22 13:11

Nikhil Shah


In my case, i forgot remove component from default route

{ path: '', redirectTo: '/home', component: MovieComponent, pathMatch: 'full' },
{ path: 'home', component: MovieComponent }

Just remove component: MovieComponent from first line

like image 2
otaviodecampos Avatar answered Nov 19 '22 13:11

otaviodecampos