Angular專案下一個路由如何接收到上一個路由url中的引數id

琪琪的爸爸發表於2019-02-28

1.父路由routes配置方式:

export const  EvaluationQuestionnaireRoute=[

    {

    path:':edit/:id', //必須是路徑名:/id

    component:EditQuestionnaireComponent

    },

    2.跳轉傳參方式:

模板:

<div [routerLink]="['edit',1]">頁面跳轉傳值</div>

控制器:

import { Router } from '@angular/router';

this.router.navigateByUrl("edit/"+q.id);

this.router.navigate(["edit/",q.id]);

    3.子路由獲取父路由引數:

import { Component, OnInit } from '@angular/core';

import {AczivatedRoute} from "@angular/router";

@Component({

  selector: 'app-product',

  templateUrl: './product.component.html',

  styleUrls: ['./product.component.scss']

})

export class ProductComponent implements OnInit {

constructor(public route: ActivatedRoute) {}

  ngOnInit() {

        //通過訂閱這種形式來接收父級頁面傳過來的值                              this.activatedRoute.params.subscribe(

      data=>{

      this.id=data.id;

      console.log(data.id)}

    );

        //或者通過

    this.route.params['value']['id']

        //或者通過

    this.id = this.activatedRoute.snapshot.params['id'];

  }

 

}

相關文章