W
W
waltaki2019-08-23 19:57:44
Angular
waltaki, 2019-08-23 19:57:44

Angular route - how to check which page I'm on?

Hello.
In Angular route I have a page defined like this:

{ path: "support/:id", component: SupportViewComponent }

Everything is working. But I need to somehow check if I'm currently on path: "support/:id".
This is needed for the main component in which this one is nested. Its behavior will change slightly if I go to "support/:id".
Working option now: in the main component
public RegExpSearchNum = new RegExp(/\d/);
constructor(public route: Router) {}

<p>{{ route.url.search(RegExpSearchNum) != -1 && route.url.search('/support/') != -1 ? true : false }}</p>

Is there a nicer way to do this? Maybe this.route has some functions for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-08-24
@waltaki

Can do

this.route.events.pipe(
  map(() => this.route.currentUrlTree.root.children.primary.segments[1].path), // вместо 1 посмотреть ваш индекс
  distinctUntilChanged(), // или можно ловить только NavigationEnd
  map(path=> path === 'support')
)

And subscribe to it in the component.
doesn't look very good.
Or you can make a service of the CurrentComponentService type, inject it into your component and inform the service on ngOnInit ngOnDestroy that the component has been created and deleted. And in the main component to listen to these events.
In this option, you can not look at the routing at all, after all, it can change.
At home, I keep a service that, when routed, parses the url from this.route.routerState.snapshot.urland contains a stream with the current navigation.
By the way, you need to change it to a general service with messages, it looks more logical than looking at the router.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question