angular - Angular If 语句 - 检查路线
问题描述
我想为我的程序做一个 if 语句来检查我当前在哪个路由/路径上。
我有一个函数,这个函数应该只在我在特定路径上时才起作用。例如:如果路径是主页我在“主页”上,但是如果我在 url 中写“主页/添加”,我想调用一个函数。我试过“ if(this.router.url.include('add') { .... code ... } 但它没有用。有人可以帮我吗?谢谢!
解决方案
You have to add nested route '/add' and then in this component which will be available for 'add' route - call your function on ngOnInit(){ /// }
.
const routes: Routes = [
{
path: 'homepage',
component: DashboardComponent,
children: [
{path: 'add', component: DashboardAddComponent}
]
}
]
then in component:
class DashboardAddComponent implements OnInit {
ngOnInit() {
// ... CALL HERE
}
}
推荐阅读
- geolocation - 按地理位置列出元素
- xml - 在 SSISDB 中解密 XML
- python - 改组熊猫数据框
- python - Django 表单 ModelChoiceField 使用 __Str__ 或 label_for_Instance 又如何?
- java - Java 打印线程卡住了:为什么?
- java - 为什么 Java Interpreter 找不到我在 CLASSPATH 中指定的包?
- javascript - 只允许没有空格或数字的英文字母?
- firebase - firebase 依赖项不再起作用
- sqlalchemy - SQLAlchemy - 在多个查询中使用相同的连接
- nginx - 例程:SSL23_GET_SERVER_HELLO:sslv3 警报握手失败