Redirecting conditionally without using component Angular
up vote
1
down vote
favorite
I have a lazy module having two component, first and second. The module is loaded for a route lazy/:id
. Depending on the type of id
(an API call will return the type of id
), we have to load the first or second component. To achieve this, currently a third component named resolver is added to the lazyModule
, which loads for the path:''
route in lazyModule and makes the API call for id
. The response is appended to the url and this.router.navigate
is used to navigate to the first
or second
component.
The issue is that this functionality is being used in multiple places in the application and I would like to use a common code at a single place. Using a component with empty template doesn't appears to be a good idea. Is there any other way to achieve it maybe by using guard/service/resolver
or any other feature.
I've made a minimal reproduction for this on stackblitz: https://stackblitz.com/edit/angular-8ylxq8?
angular angular-router
add a comment |
up vote
1
down vote
favorite
I have a lazy module having two component, first and second. The module is loaded for a route lazy/:id
. Depending on the type of id
(an API call will return the type of id
), we have to load the first or second component. To achieve this, currently a third component named resolver is added to the lazyModule
, which loads for the path:''
route in lazyModule and makes the API call for id
. The response is appended to the url and this.router.navigate
is used to navigate to the first
or second
component.
The issue is that this functionality is being used in multiple places in the application and I would like to use a common code at a single place. Using a component with empty template doesn't appears to be a good idea. Is there any other way to achieve it maybe by using guard/service/resolver
or any other feature.
I've made a minimal reproduction for this on stackblitz: https://stackblitz.com/edit/angular-8ylxq8?
angular angular-router
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a lazy module having two component, first and second. The module is loaded for a route lazy/:id
. Depending on the type of id
(an API call will return the type of id
), we have to load the first or second component. To achieve this, currently a third component named resolver is added to the lazyModule
, which loads for the path:''
route in lazyModule and makes the API call for id
. The response is appended to the url and this.router.navigate
is used to navigate to the first
or second
component.
The issue is that this functionality is being used in multiple places in the application and I would like to use a common code at a single place. Using a component with empty template doesn't appears to be a good idea. Is there any other way to achieve it maybe by using guard/service/resolver
or any other feature.
I've made a minimal reproduction for this on stackblitz: https://stackblitz.com/edit/angular-8ylxq8?
angular angular-router
I have a lazy module having two component, first and second. The module is loaded for a route lazy/:id
. Depending on the type of id
(an API call will return the type of id
), we have to load the first or second component. To achieve this, currently a third component named resolver is added to the lazyModule
, which loads for the path:''
route in lazyModule and makes the API call for id
. The response is appended to the url and this.router.navigate
is used to navigate to the first
or second
component.
The issue is that this functionality is being used in multiple places in the application and I would like to use a common code at a single place. Using a component with empty template doesn't appears to be a good idea. Is there any other way to achieve it maybe by using guard/service/resolver
or any other feature.
I've made a minimal reproduction for this on stackblitz: https://stackblitz.com/edit/angular-8ylxq8?
angular angular-router
angular angular-router
asked 2 days ago
Sachin Gupta
23916
23916
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Service will work - because service will be a injector and if you just inject your service in root module
you can make use of it in any module
Like this
import Injectable from '@angular/core';
@Injectable(
providedIn: 'root',
)
export class HeroService
constructor()
You don't need to add the service in any of the module
provider
array - provideIn
will take care of injecting in your root module
Finally - you are thinking of using it in many places that too while routing so you can call this service when routing happens and navigate
to the specific components
- to access some service before routing you can use route resolver
@Injectable()
class TeamResolver implements Resolve<Team>
constructor(private backend: Backend)
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>
Insted of this.backend.fetchTem()
method you can call your service - so you can remove your component and just mapp your resolver
service
path: 'team/:id',
component: TeamCmp,
resolve:
team: TeamResolver
Way to access your resolver
in your routing !! since resolver is also a service you can use it in any of your routes
Hope this helps - happy coding !!
Already thought about this. The issue is that I am loading theResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.
– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default routepath:''
– Sachin Gupta
2 days ago
Yeah just add a property asresolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing
– Rahul Swamynathan
2 days ago
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
Service will work - because service will be a injector and if you just inject your service in root module
you can make use of it in any module
Like this
import Injectable from '@angular/core';
@Injectable(
providedIn: 'root',
)
export class HeroService
constructor()
You don't need to add the service in any of the module
provider
array - provideIn
will take care of injecting in your root module
Finally - you are thinking of using it in many places that too while routing so you can call this service when routing happens and navigate
to the specific components
- to access some service before routing you can use route resolver
@Injectable()
class TeamResolver implements Resolve<Team>
constructor(private backend: Backend)
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>
Insted of this.backend.fetchTem()
method you can call your service - so you can remove your component and just mapp your resolver
service
path: 'team/:id',
component: TeamCmp,
resolve:
team: TeamResolver
Way to access your resolver
in your routing !! since resolver is also a service you can use it in any of your routes
Hope this helps - happy coding !!
Already thought about this. The issue is that I am loading theResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.
– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default routepath:''
– Sachin Gupta
2 days ago
Yeah just add a property asresolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing
– Rahul Swamynathan
2 days ago
add a comment |
up vote
0
down vote
Service will work - because service will be a injector and if you just inject your service in root module
you can make use of it in any module
Like this
import Injectable from '@angular/core';
@Injectable(
providedIn: 'root',
)
export class HeroService
constructor()
You don't need to add the service in any of the module
provider
array - provideIn
will take care of injecting in your root module
Finally - you are thinking of using it in many places that too while routing so you can call this service when routing happens and navigate
to the specific components
- to access some service before routing you can use route resolver
@Injectable()
class TeamResolver implements Resolve<Team>
constructor(private backend: Backend)
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>
Insted of this.backend.fetchTem()
method you can call your service - so you can remove your component and just mapp your resolver
service
path: 'team/:id',
component: TeamCmp,
resolve:
team: TeamResolver
Way to access your resolver
in your routing !! since resolver is also a service you can use it in any of your routes
Hope this helps - happy coding !!
Already thought about this. The issue is that I am loading theResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.
– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default routepath:''
– Sachin Gupta
2 days ago
Yeah just add a property asresolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing
– Rahul Swamynathan
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Service will work - because service will be a injector and if you just inject your service in root module
you can make use of it in any module
Like this
import Injectable from '@angular/core';
@Injectable(
providedIn: 'root',
)
export class HeroService
constructor()
You don't need to add the service in any of the module
provider
array - provideIn
will take care of injecting in your root module
Finally - you are thinking of using it in many places that too while routing so you can call this service when routing happens and navigate
to the specific components
- to access some service before routing you can use route resolver
@Injectable()
class TeamResolver implements Resolve<Team>
constructor(private backend: Backend)
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>
Insted of this.backend.fetchTem()
method you can call your service - so you can remove your component and just mapp your resolver
service
path: 'team/:id',
component: TeamCmp,
resolve:
team: TeamResolver
Way to access your resolver
in your routing !! since resolver is also a service you can use it in any of your routes
Hope this helps - happy coding !!
Service will work - because service will be a injector and if you just inject your service in root module
you can make use of it in any module
Like this
import Injectable from '@angular/core';
@Injectable(
providedIn: 'root',
)
export class HeroService
constructor()
You don't need to add the service in any of the module
provider
array - provideIn
will take care of injecting in your root module
Finally - you are thinking of using it in many places that too while routing so you can call this service when routing happens and navigate
to the specific components
- to access some service before routing you can use route resolver
@Injectable()
class TeamResolver implements Resolve<Team>
constructor(private backend: Backend)
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any>
Insted of this.backend.fetchTem()
method you can call your service - so you can remove your component and just mapp your resolver
service
path: 'team/:id',
component: TeamCmp,
resolve:
team: TeamResolver
Way to access your resolver
in your routing !! since resolver is also a service you can use it in any of your routes
Hope this helps - happy coding !!
answered 2 days ago
Rahul Swamynathan
59516
59516
Already thought about this. The issue is that I am loading theResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.
– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default routepath:''
– Sachin Gupta
2 days ago
Yeah just add a property asresolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing
– Rahul Swamynathan
2 days ago
add a comment |
Already thought about this. The issue is that I am loading theResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.
– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default routepath:''
– Sachin Gupta
2 days ago
Yeah just add a property asresolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing
– Rahul Swamynathan
2 days ago
Already thought about this. The issue is that I am loading the
ResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.– Sachin Gupta
2 days ago
Already thought about this. The issue is that I am loading the
ResolverComponent
everytime without any use. Even with your suggestion, I would be loading TeamCmp.– Sachin Gupta
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
Make your resolver component as service - you cannot load component everytime so try with service - Thanks
– Rahul Swamynathan
2 days ago
And how to use that service when I hit the default route
path:''
– Sachin Gupta
2 days ago
And how to use that service when I hit the default route
path:''
– Sachin Gupta
2 days ago
Yeah just add a property as
resolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing– Rahul Swamynathan
2 days ago
Yeah just add a property as
resolve
in your route and map a resolver service - in that service you just call your service and try to navigate, the resolver will run first before routing– Rahul Swamynathan
2 days ago
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237913%2fredirecting-conditionally-without-using-component-angular%23new-answer', 'question_page');
);
Post as a guest
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
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
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