1

yes, there is some others similar questions like this, but my scenario is different due the structure of my app...

Basically I'm receiving this error:

EXCEPTION: Template parse errors: Can't bind to 'routerLink' since it isn't a known native property ("or="#item of menuItems; #i=index">][routerLink]="['Rooms']">{{item.label}}

I decided to place router declarations in basically all the involved files to ensure that's not a lack of missing directives of declarations.

Well, my app structure is like this:

app.component : It's calls a master template in html where my router-outlet is located. navbar.component - is where my router link is placed and causes the error

room.component - the component that's supposed to be invoked if sucesfully.

app.component - code:

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {RouterOutlet} from 'angular2/router';
import {RouterLink} from 'angular2/router';
import {NavBarComponent} from './components/navBar/navbar.component';
import {RoomsComponent} from './components/rooms/rooms.component';
@Component({
    selector: 'my-app',
    directives : [NavBarComponent,RoomsComponent,ROUTER_DIRECTIVES],
    templateUrl : 'app/templates/master.html'
})
@RouteConfig([
    {path:'/rooms', name: 'Rooms', component: RoomsComponent, useAsDefault: true}
])
export class AppComponent { }

navbar.component - code:

import {Component} from 'angular2/core';
import {RouterLink} from 'angular2/router';
import {RouterOutlet} from 'angular2/router';
@Component({
  selector: 'nav-bar',
  template : `<ul>
    <li *ngFor="#item of menuItems; #i=index"><a 
    [ngClass]="{active: activeIndex ==i}" (click)= "navigateTo(i)"  
    [routerLink]="['Rooms']">            {{item.label}}</a></li>
    <div class="exit"><i class="material-icons md-48">exit_to_app</i>    </div>
</ul>
`,
})
export class NavBarComponent {
menuItems =  [
    {
        label : 'Rooms',
        pointsTo : 'Rooms'
    },
    {
        label : 'Bookings',
        pointsTo : 'Bookings'
    },
    {
        label : 'Favourites',
        pointsTo : 'Favourites'
    }
];
activeIndex = 0;
constructor() {
    this.navigateTo = function(ndx){
        this.activeIndex = ndx;
    }
}
}

rooms.component - code

import {Component} from 'angular2/core';
import {Router, RouteParams} from 'angular2/router';
import {RouterOutlet} from 'angular2/router';
import {RouterLink} from 'angular2/router';
@Component({
    selector : 'rooms',
  template : `
    <h1>I'm the room !</h1>`,
})
export class RoomsComponent {
}

master.html - code:

<div class="appContainer">
<div class="appTop">
    <div class="logo">
        <logo></logo>
    </div>
    <div class="navBar">
        <nav-bar></nav-bar>
    </div>
    <div class="mobileMenu">
        mobile menu
    </div>
</div>
<div class="appContent">
    <div class="searchArea">
        Search
    </div>
    <div class="subjectArea">
        <router-outlet></router-outlet>
    </div>
</div>
</div>

main.ts - code:

import {bootstrap}    from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';

bootstrap(AppComponent, [ROUTER_PROVIDERS]);
Marco Jr
  • 6,496
  • 11
  • 47
  • 86

1 Answers1

2

export class NavBarComponent { is missing ROUTER_DIRECTIVES

@Component({
  selector: 'nav-bar',
  directives : [ROUTER_DIRECTIVES],
  template : `<ul>
    <li *ngFor="#item of menuItems; #i=index"><a 
    [ngClass]="{active: activeIndex ==i}" (click)= "navigateTo(i)"  
    [routerLink]="['Rooms']">            {{item.label}}</a></li>
    <div class="exit"><i class="material-icons md-48">exit_to_app</i>    </div>
</ul>
`,
})
export class NavBarComponent {

like you have them in AppComponent

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567