I have the following tab structure in my page, with it's respective URLs given in braces:
Main Tab (/page)
/ | \
/ | \
/ | \
/ | \
Sub Tab 1 Sub Tab 2 Sub Tab 3
(/page/a) (/page/b) (/page/c)
Component
@Input() route: string = '';
selectTab(tab) {
let route = this.router.url.split('?')[0];
/* Here redirectUrl = "/page/c" */
if (tab.redirectUrl.length > 0) {
// console.log(tab.redirectUr); gives /page/c
this.router.navigate([tab.redirectUrl]);
// This works but reloads the page and is a very bad idea.
// window.location.href = tab.redirectUrl;
}
}
Component.html
NOTE: The actual code has a custom element, I'm showing it as a <div>
just to show how redirectUrl
is passed.
<div route="/page" [redirectUrl]="/page/c"></div>
The selectTab
function is called when I click Main Tab. And since the Main Tab has redirectUrl
the if condition satisfies and the page goes from /page
to /page/c
. (This only works once)
But, if I click any other Sub Tab (say Sub Tab 2
with URL /page/b
), then clicking on main tab doesn't redirect to /page/c
, instead stays at /page/b
.
I have already checked the redirectUrl
inside if condition, it's still /page/c
, but redirection doesn't happen.
What should I do to force redirection?
EDIT
@HuseinRoncevic
<div *ngFor="let tab of tabs" (click)="selectTab(tab)">
EDIT
Routes
{
path: 'page',
component: MyComponent
},
{
path: 'page/:action',
component: MyComponent
}
I am not sure why your route definition points to the same component:
Why don't you create a separate component for
page/:action
route?Here is something from the Routing and Navigation documentation on Angular.io.
I believe that this is your issue. You are not really changing the components. Rather, you are reusing the same component over and over again. And for that reason router is reusing the same component instance.
For that reason I would create a new component that handles the
:action
part of your route.Update
In your
selectTab()
function, what is the purpose of splitting along the question mark?Your route will not be appended as a query parameter, but rather it will be appended to the actual route as in
http://something/page/a
nothttp://something/?page/b
or however you imagined this. Theurl
property will return the url as determined by the router:/page/a
or/page/b
. The length of your result should be 0. Because of that yourif
part will always be false and no redirection will occur.You can try this:
in HTML