路由传递参数和接受参数2

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {HomeComponent} from './home/home.component';
import {ProductComponent} from './product/product.component';
import {Code404Component} from './code404/code404.component';
const routes: Routes = [
  {
    path: '',
    component:HomeComponent,
    children: []
  },
  {
    path:'product/:id',
    component:ProductComponent
  },
  {
    path:'**',
    component:Code404Component
  }
];

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

<h1>welcome to angular4.0</h1>
<a [routerLink]="['/']">首页</a>
<a [routerLink]="['/product',1]" >商品详情页</a>
<input type="button" name="" value="商品详情" (click)='toProductDetails()'>
<router-outlet></router-outlet>

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
  private productId:number;
  constructor(private routeInfo:ActivatedRoute) { }

  ngOnInit() {
    this.productId=this.routeInfo.snapshot.params['id']
  }

}

推荐阅读更多精彩内容