简体   繁体   English

角获取当前路线

[英]Angular get the current routes

I'm trying to create the custom breadcrumb. 我正在尝试创建自定义面包屑。 I would like to get the current routes without dataid. 我想获取没有dataid的当前路由。

I have tried the below but it is coming with data ID and routing is not working. 我已经尝试过以下方法,但是它带有数据ID,并且路由不起作用。

Expert advice please. 请专家建议。

Actual link: http://localhost:4200/settings/data?dataId=1100 实际链接: http:// localhost:4200 / settings / data?dataId = 1100

HTML 的HTML

<ul >
  <li class="breadcrumb-item" *ngFor="let breadLink of breadcrumbListObj"><a [routerLink]="breadLink.link">{{breadLink.name}}</a></li>
</ul>

TS TS

 import { Component, OnInit } from '@angular/core';
    import { Location } from '@angular/common';
    import {Router, ActivatedRoute, NavigationEnd, RoutesRecognized, Params, PRIMARY_OUTLET} from '@angular/router'
    constructor(location: Location, activate: ActivatedRoute, router:Router) {

      router.events.subscribe((val) => {
      if (location.path() !== '') {
        this.route = location.path();
        this.breadcrumbListArray = this.route.split('/');
        for(let d of this.breadcrumbListArray) {
         this.breadcrumbListObj["link"] = "/" + d;
         this.breadcrumbListObj["name"] = d;
        }
      }

  });

Expected breadcrumb path is => settings/data with respective routes on click on settings 预期的面包屑路径为=>设置/数据以及单击设置时的相应路由

There are quite a few ways to get the current route without the parameters ('?dataId=1100'). 没有参数('?dataId = 1100')的方法有很多种。

These are the 2 ways you can do it with Vanilla JavaScript. 这是使用Vanilla JavaScript的2种方法。

console.log(`${location.protocol}//${location.host}${location.pathname}`);
console.log(window.location.href.split('?')[0]);

Otherwise, you can use Angular's Router module from the @angular/router package. 否则,您可以使用@angular/router包中的Angular的Router模块。

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

constructor(private router: Router) {
  console.log(this.router.url.split('?')[0]);
}

You can use: 您可以使用:

this.activate.queryParams.filter(params => params.dataId).subscribe(params => {
     console.log(params);
     this.dataId = params.dataId;
     console.log(this.dataId); 
});

Hi You would need the ActivatedRoute Object from the router and use it to get the url and strip the part you want sans the params as below. 嗨,您将需要路由器中的ActivatedRoute对象,并使用它来获取url并剥离您想要的部分,没有参数,如下所示。

constructor(private route : ActivatedRoute) { }

ngOnInit() {
 console.log(this.route.routeConfig.path.split('?')[0]);
}

You can inject the ActivatedRoute that contains information about the url segments. 您可以注入包含有关url段信息的ActivatedRoute One gotcha is that it points to the route on which the component is rendered, not the most recent route. 一个陷阱是它指向呈现组件的路线,而不是最新的路线。 I'm guessing your breadcrumb will be rendered in some root component so that it displays anywhere on the page. 我猜您的面包屑将在某个根组件中呈现,以便它显示在页面上的任何位置。 What you can do is get the children of the route and find the most recent route this way. 您可以做的是找到该路线的子级,然后以这种方式找到最新的路线。 Something like while(route.firstChild) route=route.firstChild . 类似while(route.firstChild) route=route.firstChild You will need to trigger the refresh of your breadcrumb, too. 您还需要触发面包屑的刷新。 To do that you can for example subscribe to the router events. 为此,您可以例如订阅路由器事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM