zoukankan      html  css  js  c++  java
  • angular2的get路由传值

    路由配置页

     

     html页  get的路由跳转传值

    <p>news works!</p>
    <ng-container *ngFor="let item of list;let key=index" > 
    <a [routerLink]="['./work']" [queryParams]="{aid:key,name:'张三'}">name{{key}}--->{{item}}</a>
    <button (click)="goWork(key)">跳转</button>
    <br>
    </ng-container>
     
    get 的js路由跳转传值
     
    import { Component, OnInit } from '@angular/core';
    import { Router } from '@angular/router';

    @Component({
      selector: 'app-news',
      templateUrl: './news.component.html',
      styleUrls: ['./news.component.css']
    })
    export class NewsComponent implements OnInit {

      constructor(private router: Router) { }

      ngOnInit() {
        for (let i = 0; i < 10; i++) {
          this.list.push("这是第" + i + "条数据");
        }
      }
      list: Array<string> = [];

      // goWork(id:string){
      //   this.router.navigate(['/work',id]);
      // }
      data:any={
       queryParams:{
        name:'zhangsan',
        aid:20,
       } 
      }
      goWork(){
        this.router.navigate(['/work'],this.data);

      }

    }
     
    获得路由所传的值
     
    import { Component, OnInit } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    @Component({
      selector: 'app-work',
      templateUrl: './work.component.html',
      styleUrls: ['./work.component.css']
    })
    export class WorkComponent implements OnInit {

      constructor(private route: ActivatedRoute) { }

      ngOnInit() {
     
        this.route.queryParams.subscribe(data=>{
          console.log(data);
          console.log(data.aid+"###"+data.name);
        })
      }

    }
  • 相关阅读:
    [Evolution in aciton] C#1.1=>2.0=>3.0 [Querying Collections]
    动态操作.Config文件
    设计模式代理模式(Proxy Model)
    Linq Coding Part Five (Join之内部联接查询)
    Linq Coding Part Four[Concat应用]
    [转]领域模型管理与AOP
    08端午节,休闲一下吧,来上游戏
    有关于ViewState的二种压缩方式
    AddOutParameter引发类型问题
    Linq Coding Part Two[标准查询运算符]
  • 原文地址:https://www.cnblogs.com/kukai/p/12163825.html
Copyright © 2011-2022 走看看