Angular 5-将当前路线存储在具有ID *的变量中

时间:2018-09-05 20:37:14

标签: html angular typescript

我在Angular中使用路由来提取url并将其存储在全局变量中。它工作得很好,但URL中包含“ id”时除外。

例如,我的网址=='/ site / 1'。...

 this.authService.current_route = this.router.url
 // '/site'  (not 'site/1' or 'site/:id')

我该如何重构才能使它正常工作?

我的html看起来像:

<span id="sitehead"> <a [routerLink]="['/site', site.id ]" (click)="changeroute()"> Site:</a></span>

我的组件

import { Component, OnInit, Input } from '@angular/core';
import { DataService } from '../data.service';
import { Http } from '@angular/http';
import * as d3 from 'd3';
import { AuthService } from "../services/auth.service";
import { Router } from "@angular/router";

@Component({
  selector: 'app-summary',
  templateUrl: './summary.component.html',
  styleUrls: ['./summary.component.css'],

})
export class SummaryComponent implements OnInit {
  @Input() site;

constructor(private _dataService: DataService, private http: Http, public authService: AuthService, private router:Router ) {

}

changeroute(){
  this.authService.current_route = this.router.url
  console.log(this.authService.current_route)

}

谢谢!!让我知道是否可以澄清

1 个答案:

答案 0 :(得分:0)

可能有一种不太复杂的方法来获取当前的url,但是在过去,我创建了一个执行此操作的函数。希望它能满足您的即时需求。

let dataObject = {};
Promise.all(objectArray.map(object => {
        return new Promise(resolve => {
            yourFunction(object, anotherParameter).then(returnValue => {
                dataObject[object] = returnValue;
                resolve();
            });
        });
    })).then(() => {
        return dataObject;
    });