如何在与某个模式相对应的所有路线上隐藏一个敏捷组件?
我正在制作一个电子商务应用程序,该应用的前端是在Angular 13中制造的。UI
具有一个侧边栏,我要在产品详细信息页面上显示不是 。
为此,在app \ app.component.ts
我有:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'E-commerce';
constructor(private router: Router) {}
ngOnInit() {}
/**
* Check if the router url contains the specified route
*
* @param {string} route
* @returns
* @memberof AppComponent
*/
hasRoute(route: string) {
return this.router.url.includes(route);
}
}
in app \ app.component.html
:
<div class="app-wrapper">
<app-navbar></app-navbar>
<div class="container">
<div class="row my-3">
<div *ngIf="!hasRoute('products/show/:id')" class="col-sm-6 col-md-4 col-lg-3">
<app-sidebar class="app-sidebar"></app-sidebar>
</div>
<div class="col-sm-6 col-md-8 col-lg-9">
<router-outlet></router-outlet>
</div>
</div>
</div>
<app-footer class="app-footer"></app-footer>
</div>
出于某种原因
我无法理解,该解决方案失败,侧边栏在应用程序上的任何地方显示。
我在做什么错?
I am working on an e-commerce app who's front-end is made in Angular 13.
The UI has a sidebar which I do not want to display on the product details page.
For this purpose, in app\app.component.ts
I have:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'E-commerce';
constructor(private router: Router) {}
ngOnInit() {}
/**
* Check if the router url contains the specified route
*
* @param {string} route
* @returns
* @memberof AppComponent
*/
hasRoute(route: string) {
return this.router.url.includes(route);
}
}
In app\app.component.html
:
<div class="app-wrapper">
<app-navbar></app-navbar>
<div class="container">
<div class="row my-3">
<div *ngIf="!hasRoute('products/show/:id')" class="col-sm-6 col-md-4 col-lg-3">
<app-sidebar class="app-sidebar"></app-sidebar>
</div>
<div class="col-sm-6 col-md-8 col-lg-9">
<router-outlet></router-outlet>
</div>
</div>
</div>
<app-footer class="app-footer"></app-footer>
</div>
The problem
For a reason I have been unable to understand, this solution fails, and the sidebar is displayed anywhere on the app.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
html ^^^
ts
只需使用窗口位置来拉动路径名而不是注入路由器 - 删除构造函数注入。同样无需在此处传递道具值,因为您只有一个主张的字符串。
HTML ^^^
TS
Simply use the window location to pull the pathname instead of injecting the router - remove the constructor injection. Also no need to pass in a prop value there, because you only have a single string you are asserting.
这样,您只有一次运行该值。为了实现此目的,您可以订阅这样的路由器事件:
这样,您每次导航结束时都会更新
showbar
值。In that way, you are running that value just one time. In order to achieve this you can subscribe to the router events like this:
In this way, you are updating the
showBar
value every time the navigation end.如果有人发现它有用,这是我应用于此问题的最终解决方案:
in
app \ app.component.ts
:in
app \ app \ app.component.html
我有:请参阅“ live demo” 此处 。
In case someone may find it useful, here is the final solutions I have applied to this problem:
In
app\app.component.ts
:In
app\app.component.html
I have:See a "live demo" HERE.