发表于: 2019-03-26 21:31:16

1 714


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

Angular7添加路由及子路由方法

关键词:路由、懒加载、特性模块

在angular5以上版本官方给的方法是新建两个带路由的模块,再在模块中添加组件,这样根路由包括这两个模块,各自模块又能找到路由

ng new customer --routing

ng generate module customers –routing

ng generate component customers/customer-list

ng generate module orders –routing

ng generate component orders/order-list

--------------------------------------------------------------------------创建

<button routerLink="/customers">Customers</button>

<button routerLink="/orders">Orders</button>

<button routerLink="">Home</button>

<router-outlet></router-outlet>

找到各自模块需要在根路由中设置,注意loadChildren:
src/app/app-routing.module.ts

const routes: Routes = [

  {

    path: 'customers',

    loadChildren: './customers/customers.module#CustomersModule'

  },

  {

    path: 'orders',

    loadChildren: './orders/orders.module#OrdersModule'

  },

  {

    path: '',

    redirectTo: '',

    pathMatch: 'full'

  }

];

各自特性模块基本在创建组件时会import组件名称,基本不用管,只需在该模块的路由中添加子组件的路由:
src/app/customers/customers-routing.module.ts:

import { NgModule } from '@angular/core';

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

import { CustomerListComponent } from './customer-list/customer-list.component';

const routes: Routes = [

  {

    path: '',

    component: CustomerListComponent

  }

];

@NgModule({

  imports: [RouterModule.forChild(routes)],

  exports: [RouterModule]

})

export class CustomersRoutingModule { }

注意:path后是空格


明天计划的事情:(一定要写非常细致的内容) 

继续任务
遇到的问题:(遇到什么困难,怎么解决的) 

ng7的写法问题,比如在跨域过后,res.code规定的类型是没有的,原来这样写是没有问题的,现在在要在es5文件里面添加code:类型才不会报错 ,这个办法不知道对不对
收获:(通过今天的学习,学到了什么知识)

如上


返回列表 返回列表
评论

    分享到