Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Router error, Route config should contain exactly one "component", "loader", or "redirectTo" property

Tags:

angular

This is my setup:

import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
import {LocationStrategy, APP_BASE_HREF, HashLocationStrategy, ROUTER_DIRECTIVES, Router, RouteConfig, ROUTER_PROVIDERS} from 'angular2/router';
import {HomeComponent} from "../components/HomeComponent";
import {provide} from "angular2/core";

@Component({
    selector: 'app',
    template: `<a [routerLink]="['/Home']">Home</a>
              <router-outlet></router-outlet>`,
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
    { path: '/', component: HomeComponent, name: 'HomeComponent' }
])
class RootComponent {
    constructor(router:Router) {

    }
}

bootstrap(RootComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})
    , provide(APP_BASE_HREF, {useValue: '/'})]);

and I keep getting this error, any help is appreciated

enter image description here

I am using latest Beta.0

regards

Sean

like image 651
born2net Avatar asked Dec 18 '15 01:12

born2net


2 Answers

I had met same issue,but I resolved by using the same component name used in the component definition and when import the component,eg:

definition

import {Component} from 'angular2/core';

@Component({
    selector: 'register-page',
    template: `
        <h4>Register</h4>

    `
})
export class RegisterPageComponent {
   //public hero: Hero;
}

import

import {RegisterPageComponent} from './register.component';

@RouteConfig([
   {path: '/register',   name: 'Register',component: RegisterPageComponent}
])

I met the same issue when I tried to change the component name on import eg: issue

import {RegisterComponent} from './register.component'; 

/*remove`Page` from the name ,it should be RegisterPageComponent*/

route

@RouteConfig([
    {path: '/register',name:'Register',component:RegisterComponent}
])
like image 184
Sajin M Aboobakkar Avatar answered Sep 28 '22 17:09

Sajin M Aboobakkar


I had the same issue. My import was not surrounded by curly braces, so it was undefined. The problem is the cryptic error message, which should state that the component you provided is undefined for the @RouteConfig.

like image 35
VuesomeDev Avatar answered Sep 28 '22 18:09

VuesomeDev