• [Angular 2] Child Router


    Benefit to use child router is Angualr 2 then can lazy load the component on demand. 

    Define a child router by adding three dots `/characters/...`:

    @RouteConfig([
      { path: '/characters/...', name: 'Characters', component: CharactersComponent, useAsDefault: true },
      { path: '/vehicles/...', name: 'Vehicles', component: VehiclesComponent }
    ])

    So both `characters` and `vehicles` component container child router.

    Then in each component, we define its child rotuer

    import { Component } from 'angular2/core';
    import { RouteConfig, ROUTER_DIRECTIVES } from 'angular2/router';
    
    import { CharacterComponent } from './character.component';
    import { CharacterListComponent } from './character-list.component';
    import { CharacterService } from './character.service';
    
    @Component({
      selector: 'story-characters-root',
      template: `
        <router-outlet></router-outlet>
      `,
      directives: [ROUTER_DIRECTIVES]
    })
    @RouteConfig([
      { path: '/', name: 'Characters', component: CharacterListComponent, useAsDefault: true },
        { path: '/:id', name: 'Character', component: CharacterComponent }
    ])
    export class CharactersComponent { }
    import { Component, OnInit } from 'angular2/core';
    import { RouteConfig, ROUTER_DIRECTIVES } from 'angular2/router';
    
    import { VehicleListComponent } from './vehicle-list.component';
    import { VehicleComponent } from './vehicle.component';
    import { VehicleService } from './vehicle.service';
    
    @Component({
      selector: 'story-vehicles-root',
      template: `
        <router-outlet></router-outlet>
      `,
      directives: [ROUTER_DIRECTIVES],
      providers: [VehicleService]
    })
    @RouteConfig([
      { path: '/', name: 'Vehicles', component: VehicleListComponent, useAsDefault: true },
        { path: '/:id', name: 'Vehicle', component: VehicleComponent }
    ])
    export class VehiclesComponent { }

    ----------------

    The list component, set routerLink:

    <ul class="characters">
        <li *ngFor="#character of characters | async">
          <a href="" [routerLink]="['Character', {id: character.id}]">
           {{character.id}}. {{character.name}}
          </a>
        </li>
      </ul>
  • 相关阅读:
    综合实例-文本框类部件
    输入掩码
    WebP,Jpeg,Png格式图片的编解码区别
    美团点评提前批面经
    Android中AIDL的使用详解,如何发起回调?
    JNI在Android中的实践NDK:Hello World
    Timer实现原理
    百度秋招凉经:客户端测开
    阿里秋招面经:Android客户端开发工程师
    Picasso和Glide区别
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5406643.html
Copyright © 2020-2023  润新知