Angular 6 Fallback/Redirect if param is undefined
up vote
2
down vote
favorite
I'd like to have routes set like this:
Base Route: /events/:date
A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.
Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.
I just don't know how to apply it to my needs.
Thanks
angular angular-router
add a comment |
up vote
2
down vote
favorite
I'd like to have routes set like this:
Base Route: /events/:date
A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.
Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.
I just don't know how to apply it to my needs.
Thanks
angular angular-router
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'd like to have routes set like this:
Base Route: /events/:date
A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.
Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.
I just don't know how to apply it to my needs.
Thanks
angular angular-router
I'd like to have routes set like this:
Base Route: /events/:date
A click event redirects to /events, then the routes file would have to fallback to /events/moment().format('YYYY-MM-DD') to render EventsComponent.
Inside EventsComponent you have an action to change date, which navigates according to the day you selected, in this case the routes file wouldn't need to fallback, since the :date is defined.
I just don't know how to apply it to my needs.
Thanks
angular angular-router
angular angular-router
asked Nov 9 at 16:54
João Ghignatti
18911
18911
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:
{
path: 'events',
component: EventsComponent
},
{
path: 'events/:date',
component: EventsDetailComponent,
canActivate: EventDefinedGuard
}
where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a:date, if it's not defined, to renderEventsComponent, but if it is defined I just need to renderEventsComponent. I need that cause the part of my app that redirects toeventscannot be changed right now to force a "first value" ofmoment().format('YYYY-MM-DD'). I can only change the action part insideEventsComponentto change the date selected.
– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:
{
path: 'events',
component: EventsComponent
},
{
path: 'events/:date',
component: EventsDetailComponent,
canActivate: EventDefinedGuard
}
where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a:date, if it's not defined, to renderEventsComponent, but if it is defined I just need to renderEventsComponent. I need that cause the part of my app that redirects toeventscannot be changed right now to force a "first value" ofmoment().format('YYYY-MM-DD'). I can only change the action part insideEventsComponentto change the date selected.
– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
add a comment |
up vote
0
down vote
If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:
{
path: 'events',
component: EventsComponent
},
{
path: 'events/:date',
component: EventsDetailComponent,
canActivate: EventDefinedGuard
}
where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a:date, if it's not defined, to renderEventsComponent, but if it is defined I just need to renderEventsComponent. I need that cause the part of my app that redirects toeventscannot be changed right now to force a "first value" ofmoment().format('YYYY-MM-DD'). I can only change the action part insideEventsComponentto change the date selected.
– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
add a comment |
up vote
0
down vote
up vote
0
down vote
If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:
{
path: 'events',
component: EventsComponent
},
{
path: 'events/:date',
component: EventsDetailComponent,
canActivate: EventDefinedGuard
}
where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.
If you just need to avoid navigation when the date parameter is not defined then you should have 2 routes and add a canActivate guard to the second:
{
path: 'events',
component: EventsComponent
},
{
path: 'events/:date',
component: EventsDetailComponent,
canActivate: EventDefinedGuard
}
where EventDefinedGuard is a router guard that check for the param to be defined, otherwise redirects.
answered Nov 9 at 17:29
Massimiliano Sartoretto
1818
1818
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a:date, if it's not defined, to renderEventsComponent, but if it is defined I just need to renderEventsComponent. I need that cause the part of my app that redirects toeventscannot be changed right now to force a "first value" ofmoment().format('YYYY-MM-DD'). I can only change the action part insideEventsComponentto change the date selected.
– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
add a comment |
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a:date, if it's not defined, to renderEventsComponent, but if it is defined I just need to renderEventsComponent. I need that cause the part of my app that redirects toeventscannot be changed right now to force a "first value" ofmoment().format('YYYY-MM-DD'). I can only change the action part insideEventsComponentto change the date selected.
– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.
– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a
:date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.– João Ghignatti
Nov 9 at 17:35
Hi, thanks for your answer, but maybe I wasn't that clear. I need to force a redirect to add a
:date, if it's not defined, to render EventsComponent, but if it is defined I just need to render EventsComponent. I need that cause the part of my app that redirects to events cannot be changed right now to force a "first value" of moment().format('YYYY-MM-DD'). I can only change the action part inside EventsComponent to change the date selected.– João Ghignatti
Nov 9 at 17:35
Ok so you have only one route and need to make sure there's always a parameter when going to
/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.– Massimiliano Sartoretto
Nov 9 at 17:43
Ok so you have only one route and need to make sure there's always a parameter when going to
/events/:date. You might try to add a canActivate route on a top route (base route maybe) where you inspect the destination url and eventually check the event parameter, if that's undefined you can just redirect setting a parameter.– Massimiliano Sartoretto
Nov 9 at 17:43
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
Do you mind editing your answer with updated code so I can accept it?
– João Ghignatti
Nov 17 at 15:08
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230135%2fangular-6-fallback-redirect-if-param-is-undefined%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown