我试图在Angular2中制作路由器,
我创建了一个app.routing.ts
文件,看起来像这样:
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import { NewNoteComponent } from './components/new-note.component';
import { NoteListComponent } from './components/note-list.component';
const routes: Routes =[
{path: '', pathMatch: '', redirectTo: 'note-list'},
{path: 'note-list', component: NoteListComponent},
{path: 'new-note', component: NewNoteComponent}
];
NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule{}
export const routingComponents = [NewNoteComponent, NoteListComponent];
并设置我的app.module.ts
:
import { NgModule } from '@angular/core';
import {HttpModule} from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import { AppComponent } from './app.component';
import {AppRoutingModule, routingComponents} from './app.routing';
import { SearchComponent} from './components/search.component';
@NgModule({
imports: [ BrowserModule , FormsModule, AppRoutingModule, HttpModule],
declarations: [ AppComponent, SearchComponent, NoteListComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我的问题是,当我尝试运行这个东西时,它会给我一个奇怪的错误
多数民众赞成Unexpected value 'AppRoutingModule' imported by the module 'AppModule'
这应该是真的很容易,但我真的在想我刚才犯的那个愚蠢的错误。
答案 0 :(得分:1)
@
之前AppRoutingModule
内遗失了NgModule
。
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import { NewNoteComponent } from './components/new-note.component';
import { NoteListComponent } from './components/note-list.component';
const routes: Routes =[
{path: '', pathMatch: '', redirectTo: 'note-list'},
{path: 'note-list', component: NoteListComponent},
{path: 'new-note', component: NewNoteComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule{}
export const routingComponents = [NewNoteComponent, NoteListComponent];
您应该在routingComponents
AppModule
:
declarations
@NgModule({
imports: [ BrowserModule , FormsModule, AppRoutingModule, HttpModule],
declarations: [ AppComponent, SearchComponent, routingComponents],
bootstrap: [ AppComponent ]
})
export class AppModule { }