当我尝试加载我的角度2.0应用程序时,我收到以下错误: (索引):21错误:错误:模块'AppModule'导入的意外值'[object Object]'
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { searchComponent } from './search.Component';
import { landingComponent } from './landing.Component';
export const routes: Routes = [
{
path: '',
component: searchComponent
},
{
path: 'search',
component: searchComponent
}];
export const routedComponents = [searchComponent, landingComponent];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
的AppModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { landingComponent } from './landing.Component';
import { searchComponent } from './search.Component';
import { routes, routedComponents } from './app.routing';
import { homeScript } from './Services/homeScript';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
routes
],
declarations: [
landingComponent,
searchComponent,
routedComponents
],
providers: [
homeScript
],
bootstrap: [landingComponent]
})
export class AppModule { }
键入用于启动的脚本
///<reference path="./../typings/globals/core-js/index.d.ts"/>
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './appModule';
platformBrowserDynamic().bootstrapModule(AppModule)
.then(success => console.log(`Bootstrap success`))
.catch(error => console.log('GUY ' + error));
如果我从导入中移除“路线”,则会加载着陆页但没有 任何错误。我怀疑路由中的错误,因为如果我删除了 appModule中的'routes'正确加载着陆页。我试了很多 变化,但我无法确定问题的原因。 任何帮助将不胜感激。
答案 0 :(得分:11)
问题是您将routedComponents设置为声明的一部分。 由于这不是指令,组件或管道,因此您会收到此异常。 从模块 声明 数组中删除 routedComponents ,它将解决您的问题。
答案 1 :(得分:3)
只需删除&#34;路线&#34;从导入数组并添加&#34;路由&#34;对此:
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
]
答案 2 :(得分:0)
app.menurouting.ts
export const menurouting:Routes=[{path:'',redirectTo:'menu',pathMatch:'full'},
{path:'restarenttype',component:RestaurantTypeComponentComponent},
{path:'table',component:TablelayoutComponent},
{path:'blog',component:ViewmenuComponent},
];
export const routes:ModuleWithProviders=RouterModule.forRoot(menurouting)
app.module.ts
import {routes} from './app.menurouting';
@NgModule({
declarations: [
AppComponent,
LoginComponentComponent,
TablelayoutComponent,
ViewmenuComponent,
RestaurantTypeComponentComponent
],
imports: [
BrowserModule,
routes
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 3 :(得分:-2)
确保模块已正确定义为@NgModule
并注意您的课程拼写,以确保它们匹配。
请注意,这是Angular 2的最终版本。
另请参阅此主题以获得进一步的讨论: Angular 2 Release "Unexpected value 'ElementRef' imported by the module"