raghav
raghav

Reputation: 195

how to route through different component or pages in angular2

I am new in angular2 and I am trying to route through my component but I am getting error:

enter image description here

I know that the path is getting wrong but I don't know how to mention it in my config. below is my app.routing.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { IndexComponent } from './home/index.component';
import { CarouselComponent } from './shared/carousel/carousel.component';
import { HeaderComponent } from './shared/header/header.component';
import { FooterComponent } from './shared/footer/footer.component';
import { MerchMngDashboardComponent } from './merchmng/dashboard/dashboard.component';

export const welcomeroutes: Routes = [
    { path: '', component: CarouselComponent, pathMatch: 'full' },
    { path: '', component: HeaderComponent, outlet: 'header' },
    { path: 'login', component: LoginComponent },
    { path: 'index', component: IndexComponent }
];
export const appRoutes: Routes = [
    { path: '', component: CarouselComponent, pathMatch: 'full' },
    { path: 'dashboard', component: MerchMngDashboardComponent },

]

;

this is my folder structure:

enter image description here

this is how I am using it in my index.component.html

  <div class="dvIndexInnerHeader">
                        <a class="" [routerLink]="['dashboard']">MM</a>
                    </div>

and my index.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { Routes } from '@angular/router';
import { APP_BASE_HREF } from '@angular/common';
import { HttpModule } from '@angular/http';
import { appRoutes } from '../app.routing';
import { IndexComponent } from './index.component'
@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, appRoutes],
    declarations: [IndexComponent],
    exports: [IndexComponent]
})
export class IndexModule { }

Please help

Upvotes: 0

Views: 256

Answers (1)

Sudeep Mukherjee
Sudeep Mukherjee

Reputation: 176

In your index.module.ts you can not pass your routes directly like that inside the imports. You need pass it like this:

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(appRoutes)],
    declarations: [IndexComponent],
    exports: [IndexComponent]
})

I think this was only wrong with your code. I hope my answer was helpful. :)

Upvotes: 1

Related Questions