Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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
如果我有3个URL使用相同的angular2组件,如何检测差异?_Angular_Angular2 Routing - Fatal编程技术网

如果我有3个URL使用相同的angular2组件,如何检测差异?

如果我有3个URL使用相同的angular2组件,如何检测差异?,angular,angular2-routing,Angular,Angular2 Routing,我希望/foo/add、/foo/id/1234和/foo/modify/1234都使用相同的angular2组件 {path: '/foo/id/:id', name: 'FooView', component: FooComponent}, {path: '/foo/modify/:id', name: 'FooModify', component: FooComponent}, {path: '/foo/add', name: 'FooAdd', component: FooCompone

我希望/foo/add、/foo/id/1234和/foo/modify/1234都使用相同的angular2组件

{path: '/foo/id/:id', name: 'FooView', component: FooComponent},
{path: '/foo/modify/:id', name: 'FooModify', component: FooComponent},
{path: '/foo/add', name: 'FooAdd', component: FooComponent},
FooComponent
中,我如何判断要使用、查看、修改或编辑这三种状态中的哪一种?最好的方法是只注入
路由器
,并对所有3种状态使用
路由器.isRouteActive(Router.generate(['FooView'])

实现传递下一个和上一个指令。这将允许确定指向此组件的路径

另一种方法是在发货时获取当前的说明

目前,您还可以创建一个订阅路由更改并允许组件获取当前路径的全局服务

@Injectable() 
export class RouterInfoService {
  currentRoute;
  constructor(private router:Router) {
    router.subscribe(value => { /* store route updates locally */ }
  }
}

@Component( ... )
class MyComponent {
  constructor(private routerInfoService:RouterInfoService) {}

  someFunc() {
    if(this.routerInfoService.currentRoute == ...) {
    }
  }
}
单程

{path: '/foo/:action/:id', name: 'Foo', component: FooComponent}

然后,您可以检查
操作
参数以及
id
参数。

在问题的最后一行,个人感觉问题有点混乱。@PankajParkar这更有意义吗?