html - 隐藏特定路线Angular6上的搜索栏
问题描述
我想在路由是时在我的标题上显示一个输入,/home
并在它是其他东西时隐藏它。这是我现在尝试过的,但它不能正常工作。
.html
<div *ngIf="searchBarVisible">
<input class="form-control" placeholder="Search">
</div>
.ts
searchBarVisible: boolean = false
constructor(private router: Router) {
router.events.forEach((event) => {
if (event instanceof NavigationStart) {
if (event['url'] == '/home') {
this.searchBarVisible = true
} else {
this.searchBarVisible = false
}
}
});
}
哪个似乎是问题?感谢您的时间!
解决方案
您可以检查router.url
如下:
isHome(): boolean {
const check = this.router.url.indexOf('/home');
if (check) {
return true;
}
return false;
}
进而,
<div *ngIf="isHome()">
<input class="form-control" placeholder="Search">
</div>
推荐阅读
- python - 初学者 OOP 问题....为什么在其他函数中使用“自我”(不仅仅是 __init___)
- reactjs - Reactjs:TypeError:无法在“窗口”上执行“获取”:名称无效
- php - 首先尝试保护文件不被访问
- javascript - 为 Redux Store 定义 initialState 的不同方法
- web-scraping - 如何知道在 puppeteer 中提交的表单是否没有错误
- python - 如何为运行 gcc 命令的 docker 容器分配更多内存?
- facebook - 似乎无法弄清楚 Facebook 在重定向方面想从我这里得到什么?
- amazon-web-services - 在 AWS DynamoDB 中,为什么我的 ConsistentRead 没有为我提供最新数据?
- css - 引导程序和 CSS
- prolog - 如何在序言中获得曾曾祖母