首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有RouterOutletMap的提供程序

没有RouterOutletMap的提供程序
EN

Stack Overflow用户
提问于 2016-09-17 19:24:47
回答 2查看 10.9K关注 0票数 12

最新的Angular 2.0.0,通过最新的angular-cli 1.0.0-beta.14,node: 6.6.0,os: linux x64

我的工作是:

1)创建新项目

代码语言:javascript
复制
ng new angular-test
ng g component projects
ng g component typings

2)添加简单路由

/src/app/app.component.html

代码语言:javascript
复制
 <router-outlet></router-outlet>

/src/app/app.module.ts

代码语言:javascript
复制
export const ROUTES: Routes = [
  {
    path: '',
    redirectTo: '/projects',
    pathMatch: 'full'
  },
  {
    path: 'projects',
    component: ProjectsComponent,
  },
  {
    path: '/typings',
    component: TypingsComponent
  },
  {
    path: '**', redirectTo: ''
  }
];

@NgModule({
  declarations: [
    AppComponent,
    ProjectsComponent,
    TypingsComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule.forChild(ROUTES)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我得到的是:

代码语言:javascript
复制
EXCEPTION: Error in ./AppComponent class AppComponent - inline template:3:0 caused by: No provider for RouterOutletMap!
ORIGINAL EXCEPTION: No provider for RouterOutletMap!

我是如何尝试解决这个问题的

我尝试在AppModule中添加RouterOutletMap到提供者,异常不会抛出,但是应用不会重定向到项目,也不会显示嵌套组件

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-17 19:46:08

您需要为应用程序模块调用RouterModule.forRoot,而不是forChild。以前的adds all the core providers,而the latter doesn't。您应该对子模块使用forChild,而不是应用程序模块。

票数 27
EN

Stack Overflow用户

发布于 2017-06-04 00:10:36

我也有这个问题。这就是我如何避免……总有一天这会有所帮助。谨致问候!

路由阵列-- app.routes.ts

代码语言:javascript
复制
const appRoutes:Routes =[
 {
   path: 'home',
   component: CarouselComponent
 },
 {
  path: 'profile',
  component:UserDashboardComponentComponent,
  children: [
     {
      path: 'overview',
      component: ProfileOverviewComponent
     },
     {
      path: 'post',
      component: PostAddComponent
     }
    ]
   }
];

 export const WebRouting: ModuleWithProviders =RouterModule.forRoot(appRoutes); 

app.module.ts

代码语言:javascript
复制
@NgModule({
 declarations: [
  AppComponent,
  CarouselComponent,
  .......
],
 imports: [
  BrowserModule,
  MdTabsModule,
  MaterialModule,
  MdInputModule,
  BrowserAnimationsModule,
  FormsModule,
  HttpModule,
  WebRouting
],
 providers: [],
 bootstrap: [AppComponent],
})

export class AppModule { }

tempale.html

代码语言:javascript
复制
  <li>
    <a [routerLink]="['/profile/overview']" routerLinkActive="active" > 
      Overview
    </a>
  </li>
  <li>
    <a [routerLink]="['/profile/post']"routerLinkActive="active">
      PostAdd
    </a>
  </li>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39546278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档