美文网首页
路由传递参数和接受参数1

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

作者: c59ffede9db6 | 来源:发表于2017-05-14 15:10 被阅读0次
    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/',
        component:ProductComponent
      },
      {
        path:'**',
        component:Code404Component
      }
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }
    
    
    
    <a [routerLink]="['/']">首页</a>
    <a [routerLink]="['/product']" [queryParams]='{id: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.queryParams['id']
      }
    
    }
    
    
    <p>
      这是product组件
    </p>
    <p>
    商品id是{{productId}}
    </p>
    
    

    相关文章

      网友评论

          本文标题:路由传递参数和接受参数1

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