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?










share|improve this question

























    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?










    share|improve this question























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Sachin Gupta

      23916




      23916






















          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 !!






          share|improve this answer




















          • 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










          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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 !!






          share|improve this answer




















          • 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










          • 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














          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 !!






          share|improve this answer




















          • 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










          • 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












          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 !!






          share|improve this answer












          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 !!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Rahul Swamynathan

          59516




          59516











          • 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










          • 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
















          • 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










          • 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















          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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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














































































          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto