美文网首页
angular中子路由的配置

angular中子路由的配置

作者: nzjcnjzx | 来源:发表于2018-12-10 17:05 被阅读0次

新建项目

  • ng new project

新建组件

  • ng g c home
  • ng g c some
  • ng g c some/child1
  • ng g c some/child2
  • 在some中新建模块文件和路由文件结构如下:


    image.png

在some.module.ts文件中

  • 引入子组件注入路由模块
import { SomeComponent } from './some.component';
import { Child2Component } from './child2/child2.component';
import { Child1Component } from './child1/child1.component';
import { NgModule } from '@angular/core';
import { SomeRoutingModule } from './some.routing';


@NgModule({
    declarations: [
       Child1Component,
       Child2Component ,
       SomeComponent
    ],
    imports: [
        SomeRoutingModule
    ]
})

export class SomeModule {}

在some.routing.ts模块中

  • 引入路由模块和子组件配置子路由导出
import { Child2Component } from './child2/child2.component';
import { Child1Component } from './child1/child1.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SomeComponent } from './some.component';

const route: Routes = [
    {
        path: '',
        component: SomeComponent,
        children: [
            {path: 'child1', component: Child1Component},
            {path: 'child2', component: Child2Component}
        ]
    }
]

@NgModule({
    imports: [
        RouterModule.forChild(
            route
        )
    ],
    exports: [
        RouterModule
    ]
})

export class SomeRoutingModule {}

在app-routig.module.ts中通过loadChildren导入进来,这里可以是从app开始的路径也可以是相对路径

import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {path: '', component: HomeComponent},
  {path: 'some', loadChildren: './some/some.module#SomeModule'}
    // 'app/some/some.module#SomeModule' 这样也可以

];

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

在app.module.ts文件中不需要引入关于子组件相关的组件和模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • 在主路由使用RouterModule.forRoot,在子路由使用RouterModule.forChild

demo地址

相关文章

  • angular中子路由的配置

    新建项目 ng new project 新建组件 ng g c home ng g c some ng g c s...

  • Augular 路由

    ### 路由配置 ``` import { NgModule } from '@angular/core'; im...

  • angular2:路由器的使用

    Angular路由器是一个可选的外部Angular ngModule,通过配置和使用路由器,页面可以根据url来切...

  • 面试知识6Angular(模拟面试)

    一:angular路由实现原理 ng-route:主要是通过$routeProvider(配置路由的映射)搭配ng...

  • angular路由

    写在前面:请注意这里是angular1.0的路由配置。 最近脑抽,好多东西都想不起来了……比如angular路由。...

  • angular 路由配置

    1.路由配置 创建两个组件 在app-routing.modult.ts中配一个空路由 在做路由配置时path属性...

  • angular 路由 ui-router

    UI-router 安装:npm install --save angular-ui-router 配置路由状态 ...

  • angular2:路由参数的传递和接收

    首先回顾一下如何配置路由: import { RouterModule } from '@angular/rout...

  • angular配置路由(二)

    一:在已存在的项目中配置路由:1,新建TS文件,命名为app-routing。 2,在当前目录下新建三个不同的组件...

  • angular配置路由(一)

    在新项目中:1,新建项目1.1:ng g demo--routing (带路由的项目),然后新建几个组件,用于后面...

网友评论

      本文标题:angular中子路由的配置

      本文链接:https://www.haomeiwen.com/subject/xjhdhqtx.html