简体   繁体   English

如何在 Angular CLI 1.1.1 版中将路由模块添加到现有模块?

[英]How to add a routing module to an existing module in angular CLI version 1.1.1?

I have created a module in using angular CLI and forget to add --routing while it's creation, now i want to add a routing module to it.我已经使用 angular CLI 创建了一个模块,但在创建时忘记添加 --routing,现在我想向其中添加一个路由模块。

ng init --routing won't help in angular CLI version 1.1.1 ng init --routing在 angular CLI 版本 1.1.1 中无济于事

That's not a problem at all.那根本不是问题。

Just add the routing manually.只需手动添加路由。

All that the --routing flag does, in addition to adding <router-outlet></router-outlet> in app.component.html , is add q RouterModule.forRoot() call in a separate module called AppRoutingModule , and include that module in the imports of AppModule . --routing标志所做的一切,除了在app.component.html中添加<router-outlet></router-outlet>之外,还包括在名为AppRoutingModule的单独模块中添加 q RouterModule.forRoot()调用,并将其包括在内模块在AppModuleimports中。

So, in app.module.ts , it adds AppRoutingModule to the imports of your AppModule .因此,在app.module.ts中,它将AppRoutingModule添加到您的AppModuleimports中。

The AppRoutingModule is defined as app-routing.module.ts , which is created from this template . AppRoutingModule被定义为app-routing.module.ts ,它是从这个模板创建的。

It's very small that I'll copy it here:它很小,我会在这里复制它:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    children: []
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

The {path: '', children:[]} is just a placeholder that you are expected to replace (I know because I added it to the source code). {path: '', children:[]}只是一个您应该替换的占位符(我知道是因为我将它添加到源代码中)。 It just makes sure the router doesn't complain about the <router-outlet> when you don't have any routes.当您没有任何路由时,它只是确保路由器不会抱怨<router-outlet>

So, back to the question, what should you do?那么,回到问题,你应该怎么做? Either copy the file to your project, to look just like the original CLI, and add AppRoutingModule to your AppModule 's imports ...要么将文件复制到您的项目,使其看起来像原始的 CLI,然后将AppRoutingModule添加到您的AppModuleimports中......

Or just add RouterModule.forRoot([/* routes here */]) to your AppModule 's imports instead.或者只是将RouterModule.forRoot([/* routes here */])添加到AppModuleimports中。 It'll still work, with built-in support for lazy loading and everything else working just fine.它仍然可以工作,内置了对延迟加载的支持,其他一切都工作得很好。

PS聚苯乙烯

Remember that to add child routes, you can always generate more modules with routing ng generate module Foo --routing regardless of whether you used the --routing flag with ng new or not.请记住,要添加子路由,您始终可以使用 routing ng generate module Foo --routing生成更多模块,无论您是否将--routing标志与ng new一起使用。 No configuration or any magic to worry about.无需担心配置或任何魔法。

  1. ng generate module app-routing --module app --flat
    • Replace app with the name of your existing moduleapp替换为您现有模块的名称
    • Replace app-routing with the name to give the routing moduleapp-routing替换为名称以提供路由模块
  2. In the new module, add appRoutes and RouterModule.forRoot or RouterModule.forChild .在新模块中,添加appRoutesRouterModule.forRootRouterModule.forChild

     import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Routes, RouterModule } from '@angular/router'; const appRoutes: Routes = [ { path: 'some-path', component: SomeComponent } ]; @NgModule({ imports: [ RouterModule.forRoot(appRoutes) ], exports: [ RouterModule ] }) export class AppRoutingModule {}
  3. Add <router-outlet></router-outlet> to your component if needed (eg, app.component.html )如果需要,将<router-outlet></router-outlet>添加到您的组件(例如app.component.html

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM