如何在angular2中路由不同的组件或页面

时间:2018-02-23 06:55:49

标签: angular2-routing

我是angular2中的新手,我正试图通过我的组件路由,但是我收到了错误:

enter image description here

我知道路径出错但我不知道如何在我的配置中提及它。下面是我的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 },

]

这是我的文件夹结构:

enter image description here

这就是我在index.component.html

中使用它的方法
  <div class="dvIndexInnerHeader">
                        <a class="" [routerLink]="['dashboard']">MM</a>
                    </div>

和我的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 { }

请帮忙

1 个答案:

答案 0 :(得分:1)

在index.module.ts中,您无法像导入中那样直接传递路径。你需要像这样传递它:

http://localhost:3000/records

我认为这只是你的代码的错误。我希望我的回答很有帮助。 :)