簡體   English   中英

由angular2中的模塊'AppModule'導入的意外值XXXX

[英]Unexpected value XXXX imported by the module 'AppModule' in angular2

我試圖在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'

假設這確實很容易,但是我確實在思考我剛才所做的愚蠢錯誤。

您在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];

並且您應該在AppModule declarations使用routingComponents

@NgModule({
  imports: [ BrowserModule , FormsModule, AppRoutingModule, HttpModule],
  declarations: [ AppComponent,  SearchComponent, routingComponents],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM