简体   繁体   English

在侧边栏上的路由 angular 中使用 URL 参数

[英]Using a URL parameter in route angular on sidebar

I would like for the links on my sidebar to be dynamically generated based on the page URL / various service data.我希望根据页面 URL / 各种服务数据动态生成侧边栏上的链接。

That is to say: If I am on a property page ~/meters/meter/54321 , I would like to click on a link on the sidebar that will send me to ~/meters/meter/54321/overview .也就是说:如果我在属性页~/meters/meter/54321上,我想单击侧边栏上的链接,该链接会将我转到~/meters/meter/54321/overview

(Bonus points: If I am not on a property page, I would like the option not to show up on the sidebar, but first things first.) (加分点:如果我不在属性页上,我希望该选项不显示在侧边栏上,但首先要做的是。)

Note that I have two different ways to be getting the id - either through a MeterService or via the paramMap .请注意,我有两种不同的方法来获取id - 通过MeterService或通过paramMap

When I'm doing this on a normal component/routing module, /meters/meter/:id/overview resolves perfectly to /meters/meter/54321/overview , and I'm able to create a simple routerlink from the page.当我在普通组件/路由模块上执行此操作时, /meters/meter/:id/overview完美解析为/meters/meter/54321/overview ,并且我能够从页面创建一个简单的routerlink

The issue is that :id is not resolving to the paramMap id , rather, it is trying to send me to /meters/meter/:id/overview .问题是:id没有解析为paramMap id ,而是试图将我发送到/meters/meter/:id/overview

Is there something special about a sidebar that makes this work differently?侧边栏有什么特别之处使它的工作方式不同吗? Should I be importing something else?我应该进口其他东西吗?

Thank you!谢谢!

component.ts:

import { Component, OnInit} from '@angular/core';
import { Routes, RouterModule, ActivatedRoute } from '@angular/router';
import { MeterService } from "../../shared/services/meter.service";

declare interface RouteInfo {
    path: string;
    title: string;
}
export const ROUTES: RouteInfo[] = [
    { path: '/meters', title: 'Property List' },
    { path: '/meters/add', title: 'Add Property'},
    { path: '/meters/meter/:id/overview', title: 'Property Overview'},
];

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {
  menuItems: any[];
  meter: string;
  id: string;

  constructor(private meterService: MeterService, private route: ActivatedRoute) {
  this.id = this.route.snapshot.paramMap.get('id'); 
}

  ngOnInit() {
    this.meterService.currentMeter.subscribe(meter => {this.meter = meter; this.menuItems = ROUTES.filter(menuItem => menuItem);})
  }

component.html:

<ul class="nav">
        <li routerLinkActive="active" *ngFor="let menuItem of menuItems" class="{{menuItem.class}} nav-item">
            <a class="nav-link" [routerLink]="[menuItem.path]">
                <p>{{menuItem.title}}</p>
            </a>
        </li>
    </ul>

First, let's assuming we are on page ~/meters/meter/54321 , we can navigate to ~/meters/meter/54321/overview with首先,假设我们在页面~/meters/meter/54321上,我们可以导航到~/meters/meter/54321/overview

<a class="nav-link" routerLink="./overview">
            ...
</a>

or要么

routerLink="overview"

or even甚至

[routerLink]="['overview']"

We know how to navigate to a child route.我们知道如何导航到子路由。 But how to display this tag only on a specific page?但是如何只在特定页面上显示这个标签呢? The core of the solution can be found here https://stackoverflow.com/a/45260402 .解决方案的核心可以在这里找到https://stackoverflow.com/a/45260402

The idea is to listen to router events in your sidebar component and for each route change, hide or display the (with for example an NgIf directive).这个想法是监听侧边栏组件中的路由器事件,并针对每个路由更改隐藏或显示(例如使用 NgIf 指令)。

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

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