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 Nov 10 at 10:10









      Sachin Gupta

      36917




      36917






















          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> any
          return this.backend.fetchTeam(route.params.id);




          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
            Nov 10 at 13:02










          • Make your resolver component as service - you cannot load component everytime so try with service - Thanks
            – Rahul
            Nov 10 at 14:16










          • And how to use that service when I hit the default route path:''
            – Sachin Gupta
            Nov 10 at 14:24










          • 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
            Nov 10 at 18:05










          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















          Required, but never shown

























          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> any
          return this.backend.fetchTeam(route.params.id);




          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
            Nov 10 at 13:02










          • Make your resolver component as service - you cannot load component everytime so try with service - Thanks
            – Rahul
            Nov 10 at 14:16










          • And how to use that service when I hit the default route path:''
            – Sachin Gupta
            Nov 10 at 14:24










          • 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
            Nov 10 at 18:05














          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> any
          return this.backend.fetchTeam(route.params.id);




          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
            Nov 10 at 13:02










          • Make your resolver component as service - you cannot load component everytime so try with service - Thanks
            – Rahul
            Nov 10 at 14:16










          • And how to use that service when I hit the default route path:''
            – Sachin Gupta
            Nov 10 at 14:24










          • 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
            Nov 10 at 18:05












          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> any
          return this.backend.fetchTeam(route.params.id);




          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> any
          return this.backend.fetchTeam(route.params.id);




          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 Nov 10 at 12:39









          Rahul

          9381315




          9381315











          • 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
            Nov 10 at 13:02










          • Make your resolver component as service - you cannot load component everytime so try with service - Thanks
            – Rahul
            Nov 10 at 14:16










          • And how to use that service when I hit the default route path:''
            – Sachin Gupta
            Nov 10 at 14:24










          • 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
            Nov 10 at 18:05
















          • 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
            Nov 10 at 13:02










          • Make your resolver component as service - you cannot load component everytime so try with service - Thanks
            – Rahul
            Nov 10 at 14:16










          • And how to use that service when I hit the default route path:''
            – Sachin Gupta
            Nov 10 at 14:24










          • 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
            Nov 10 at 18:05















          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
          Nov 10 at 13:02




          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
          Nov 10 at 13:02












          Make your resolver component as service - you cannot load component everytime so try with service - Thanks
          – Rahul
          Nov 10 at 14:16




          Make your resolver component as service - you cannot load component everytime so try with service - Thanks
          – Rahul
          Nov 10 at 14:16












          And how to use that service when I hit the default route path:''
          – Sachin Gupta
          Nov 10 at 14:24




          And how to use that service when I hit the default route path:''
          – Sachin Gupta
          Nov 10 at 14:24












          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
          Nov 10 at 18:05




          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
          Nov 10 at 18:05

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          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















          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







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus