Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度If语句-检查路由_Angular_Typescript_Path_Routes - Fatal编程技术网

Angular 角度If语句-检查路由

Angular 角度If语句-检查路由,angular,typescript,path,routes,Angular,Typescript,Path,Routes,我想为我的程序执行一个if语句,以检查我当前所在的路由/路径 我有一个函数,这个函数应该只在我在特定路径上时才起作用。 例如: 如果路径是主页,我在“主页”上,但是如果我在url中写“主页/添加”,我想调用一个函数。 我试过了” if(this.router.url.include('add'){…code…}但它不起作用。 有人能帮我吗? 谢谢!您必须添加嵌套路由“/add”,然后在该组件中,该组件将可用于“add”路由-在ngOnInit(){//}上调用您的函数 const routes:

我想为我的程序执行一个if语句,以检查我当前所在的路由/路径

我有一个函数,这个函数应该只在我在特定路径上时才起作用。 例如: 如果路径是主页,我在“主页”上,但是如果我在url中写“主页/添加”,我想调用一个函数。 我试过了” if(this.router.url.include('add'){…code…}但它不起作用。 有人能帮我吗?
谢谢!

您必须添加嵌套路由“/add”,然后在该组件中,该组件将可用于“add”路由-在
ngOnInit(){//}
上调用您的函数

const routes: Routes = [
  {
    path: 'homepage',
    component: DashboardComponent,
    children: [
      {path: 'add', component: DashboardAddComponent}
    ]
  }
]
然后在组件中:

class DashboardAddComponent implements OnInit {
  ngOnInit() {
    // ... CALL HERE
  }
}