• ng-router


    基本用法

    1. 添加 AppRoutingModule
    ng generate module app-routing --flat --module=app
    

    app-routing.module.ts

    import { NgModule }             from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    
    import { FooComponent } from './foo/foo.component'
    import { BarComponent } from './bar/bar.component'
    
    const routes: Routes = [
      {
        path: 'foo',
        component: FooComponent
      },
      {
        path: 'bar',
        component: BarComponent
      }
    ]
    
    @NgModule({
      imports: [
        RouterModule.forRoot(routes)
      ],
      exports: [ RouterModule ]
    })
    export class AppRoutingModule {}
    
    

    设置路由出口:

    <h1>{{title}}</h1>
    <router-outlet></router-outlet>
    

    设置导航链接:

    <ul>
      <li>
        <a routerLink="/foo">Go Foo</a>
      </li>
      <li>
        <a routerLink="/bar">Go Foo</a>
      </li>
    </ul>
    

    路由对象

    • path
      • 不能以 / 开头
    • component

    默认路由:

    { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
    

    动态路由匹配

    动态路径配置:

    { path: 'detail/:id', component: HeroDetailComponent },
    

    导航链接:

    <a *ngFor="let hero of heroes" class="col-1-4"
        routerLink="/detail/{{hero.id}}">
    

    在组件中解析获取动态路径参数:

    import { Component, OnInit } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { Location } from '@angular/common';
    
    @Component({
      selector: 'app-user',
      templateUrl: './user.component.html',
      styleUrls: ['./user.component.css']
    })
    export class UserComponent implements OnInit {
    
      constructor(
        private route: ActivatedRoute,
        private location: Location
      ) { }
    
      ngOnInit() {
        const id = this.route.snapshot.paramMap.get('id')
        console.log(id)
      }
    
    }
    
    

    路由导航

    后退:

    this.location.back();
    
  • 相关阅读:
    CPU die
    删除binlog的方法
    mysql中bigint、int、mediumint、smallint 和 tinyint的取值范围
    Javascript中的Keycode值列表
    php5.2转向 PHP 5.3 的 PHP 开发
    linux 下查看系统内存使用情况的方法
    Kyoto Cabinet(DBM) + Kyoto Tycoon(网络层)
    window 7 下一台cp 两个mysql 配置主从
    php 序列化(serialize)格式详解
    Linux下ntpdate时间同步
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12228212.html
Copyright © 2020-2023  润新知