0

Hello All Angular2 Developers,

Here is a challenge I am facing and not knowing how to resolve it.

I have my app.component.ts file as follows:

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

import { LeftMenuComponent } from './left-menu.component';
import {HeaderComponent} from './header.component';
import {TotalContainer} from './container-total.component';

@Component({
  selector: 'my-app',
  template: `
    <myheader [display]="display"></myheader>
    <router-outlet></router-outlet>
  `,
  directives:[HeaderComponent,TotalContainer,ROUTER_DIRECTIVES]
})
export class AppComponent { 
    display = true;
}

I have written all the view of the header in header.component and loaded in HTML. In the "header.component", I am loading the "left-menu.component". In the left-menu.component, I am having hyperlinks which are to be loaded in the "app.component".

The error that I am getting is that there is "No provider for the router".

I continued basing upon the angular.io sample but could not solve it.

Here is how I wrote the code. header.component.ts

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

import {LeftMenuComponent} from './left-menu.component';

@Component({
    selector:'myheader',
    template:`
        <nav class="navbar navbar-default">
            <div>
             ....
            </div>
        </nav>
        <left-menu [class.disp]="display"></left-menu>

    `,
    directives:[ROUTER_DIRECTIVES, LeftMenuComponent]
})
export class HeaderComponent{
    @Input() display;
}

left-menu.component.ts

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

import {KeyEventsPlugin} from '@angular/platform-browser/src/dom/events/key_events';

@Component({
    selector:'left-menu',
    template:`
        <ul class="list-group">
            <li class="list-group-item">
                <span class="glyphicon glyphicon-share-alt"></span>
                <span class="badge">14</span>
                    <a [routerLink]="['/followups']" routerLinkActive="active-link">Follow Ups</a>
            </li>
        </ul>
    `,
    directives:[ROUTER_DIRECTIVES]
})
export class LeftMenuComponent{

}

app.routes.ts

import { provideRouter, RouterConfig }  from '@angular/router';
import {FollowUpsComponent} from '../components/followups.component';
import {TotalContainer} from '../components/container-total.component';

const routes: RouterConfig = [
    {
        path:'total-container',
        component:TotalContainer
    },
    {
        path:'followups',
        component: FollowUpsComponent,
    },
    {
        path:'',
        redirectTo:'/total-container',
        pathMatch:'full'
    },
    {
        path:'**',
        redirectTo:'/total-container'
    }
];


export const appRouterProviders = [
    provideRouter(routes)
];

main.ts

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './components/app.component';

import { appRouterProviders } from './routes/app.routes'; 
bootstrap(AppComponent,[
    appRouterProviders
]); 

bootstrap(AppComponent);

Can anyone help me with this.a

1 Answer 1

4

In your main.ts it looks like you call bootstrap twice accidentally:

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './components/app.component';

import { appRouterProviders } from './routes/app.routes'; 
bootstrap(AppComponent,[
    appRouterProviders
]); 

bootstrap(AppComponent); <-----delete this! 

Delete the second one and you should be good to go

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

1 Comment

That's it. Wow. Thanks very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.