当前位置:Gxlcms > JavaScript > 对angular2与共享模块进行应用

对angular2与共享模块进行应用

时间:2021-07-01 10:21:17 帮助过:8人阅读

这次给大家带来对angular2与共享模块进行应用,对angular2与共享模块进行应用的注意事项有哪些,下面就是实战案例,一起来看一下。

创建模块,用到了共享模块PostSharedModule,共享模块里面包含了2个公用的模块:文章管理模块和评论管理模块

1,创建一个模块testmodule.module.ts

  1. import { CommonModule } from '@angular/common';
  2. import { NgModule } from '@angular/core';
  3. import { RouterModule } from "@angular/router";
  4. import { <span style="color:#cc0000;"><strong>PostSharedModule </strong></span>} from '../shared/post.module';
  5. import { testModule } from './testmodule.routes';
  6. import { TestMainComponent } from './test-main/test-main.component';
  7. import { PostTableService } from '../manage/post-table/services/post-table.service';
  8. @NgModule({
  9. declarations: [
  10. TestMainComponent
  11. ],
  12. imports: [
  13. CommonModule,
  14. <span style="color:#ff0000;">PostSharedModule</span>,
  15. RouterModule.forChild(testModule)
  16. ],
  17. exports:[
  18. TestMainComponent
  19. ],
  20. providers: [
  21. PostTableService
  22. ]
  23. })
  24. export class TestModule { }

2.创建模块路由testmodule.routes.ts

  1. import { TestMainComponent } from './test-main/test-main.component';
  2. import { PostTableComponent } from '../manage/post-table/post-table.component';
  3. import { CommentTableComponent } from '../manage/comment-table/comment-table.component';
  4. export const testModule = [
  5. {
  6. path:'',
  7. component:TestMainComponent,
  8. children: [
  9. { path: '',redirectTo:'posttable/page/1',pathMatch:'full'},
  10. { path: 'posttable/page/:page', component: PostTableComponent },
  11. { path: 'commenttable/page/:page', component: CommentTableComponent },
  12. { path: '**', redirectTo:'posttable/page/1' }
  13. ]
  14. }
  15. ];

3.执行ng g c test-main,创建组件test-main,修改test-main.component.html

  1. <a routerLink="posttable/page/1" class="list-group-item"><span class="badge">10000</span>文章管理</a>
  2. <a routerLink="commenttable/page/1" class="list-group-item"><span class="badge">1000000</span>评论管理</a>

创建 共享模块post.module.ts

  1. import { NgModule } from '@angular/core';
  2. import { ModalModule } from 'ng2-bootstrap';
  3. import { PaginationModule } from 'ng2-bootstrap';
  4. import { SharedModule } from './shared.module';
  5. import { CommentTableComponent } from '../manage/comment-table/comment-table.component';
  6. import { PostTableComponent } from '../manage/post-table/post-table.component';
  7. @NgModule({
  8. imports:[
  9. SharedModule,
  10. ModalModule.forRoot(),
  11. PaginationModule.forRoot()
  12. ],
  13. declarations:[
  14. CommentTableComponent,
  15. PostTableComponent
  16. ],
  17. exports:[
  18. ModalModule,
  19. PaginationModule,
  20. CommentTableComponent,
  21. PostTableComponent
  22. ]
  23. })
  24. export class PostSharedModule {
  25. }

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Angular路由实战案例应用

Angular使用HMR代码解析

以上就是对angular2与共享模块进行应用的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行