When are routed components initialized?












0















I have the following route:



path: ':id', component: ViewBookPageComponent },


And when added it produces the error:



Error: Cannot read property 'id' of null 


I'm not performing a null check in the component since the property will be available when the component is navigated to and in this case the auth guard is also keeping rerouting to login prior to any of the book routes being shown.



This is a stacblitz demo:



https://stackblitz.com/edit/angular-ngrx-slice-demo-fork-with-id-route?file=src%2Fapp%2Fbooks%2Findex.ts



If I comment out the route, the error no longer appears, so it seems Angular is instantiating the component prior to it being routed to.










share|improve this question

























  • Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

    – Kevin
    Jan 2 at 0:05













  • Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:12











  • We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

    – Ole
    Jan 2 at 0:12
















0















I have the following route:



path: ':id', component: ViewBookPageComponent },


And when added it produces the error:



Error: Cannot read property 'id' of null 


I'm not performing a null check in the component since the property will be available when the component is navigated to and in this case the auth guard is also keeping rerouting to login prior to any of the book routes being shown.



This is a stacblitz demo:



https://stackblitz.com/edit/angular-ngrx-slice-demo-fork-with-id-route?file=src%2Fapp%2Fbooks%2Findex.ts



If I comment out the route, the error no longer appears, so it seems Angular is instantiating the component prior to it being routed to.










share|improve this question

























  • Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

    – Kevin
    Jan 2 at 0:05













  • Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:12











  • We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

    – Ole
    Jan 2 at 0:12














0












0








0








I have the following route:



path: ':id', component: ViewBookPageComponent },


And when added it produces the error:



Error: Cannot read property 'id' of null 


I'm not performing a null check in the component since the property will be available when the component is navigated to and in this case the auth guard is also keeping rerouting to login prior to any of the book routes being shown.



This is a stacblitz demo:



https://stackblitz.com/edit/angular-ngrx-slice-demo-fork-with-id-route?file=src%2Fapp%2Fbooks%2Findex.ts



If I comment out the route, the error no longer appears, so it seems Angular is instantiating the component prior to it being routed to.










share|improve this question
















I have the following route:



path: ':id', component: ViewBookPageComponent },


And when added it produces the error:



Error: Cannot read property 'id' of null 


I'm not performing a null check in the component since the property will be available when the component is navigated to and in this case the auth guard is also keeping rerouting to login prior to any of the book routes being shown.



This is a stacblitz demo:



https://stackblitz.com/edit/angular-ngrx-slice-demo-fork-with-id-route?file=src%2Fapp%2Fbooks%2Findex.ts



If I comment out the route, the error no longer appears, so it seems Angular is instantiating the component prior to it being routed to.







javascript angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:04







Ole

















asked Jan 1 at 23:35









OleOle

9,23784986




9,23784986













  • Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

    – Kevin
    Jan 2 at 0:05













  • Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:12











  • We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

    – Ole
    Jan 2 at 0:12



















  • Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

    – Kevin
    Jan 2 at 0:05













  • Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:12











  • We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

    – Ole
    Jan 2 at 0:12

















Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

– Kevin
Jan 2 at 0:05







Not quite sure what kind of routing are you expecting? When an end-user navigates to FindBookPageComponent, how can they go to ViewBookPageComponent?

– Kevin
Jan 2 at 0:05















Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

– Ole
Jan 2 at 0:12





Here's a full blown working example that I'm refactoring: stackblitz.com/edit/akita-books-store

– Ole
Jan 2 at 0:12













We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

– Ole
Jan 2 at 0:12





We first navigate to find, then search, then click on a search result to view the details. The detailed view would be /books/:id where the :id value would be used to look up the book.

– Ole
Jan 2 at 0:12












2 Answers
2






active

oldest

votes


















0














Is the following fix your problem?



  const routes: Routes = [
{
path: 'find',
component: FindBookPageComponent,
},
children:[
{
path: 'result',
component: SearchResultComponent,
},
children:[
{
path: 'book/:id',
component: ViewBookPageComponent ,
}
]
]

]





share|improve this answer


























  • This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

    – Baruch
    Jan 2 at 0:08











  • This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:10











  • I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

    – Ole
    Jan 2 at 0:11











  • Hi, @Ole. See my update. Basically, use children routing.

    – Kevin
    Jan 2 at 0:23











  • Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

    – Ole
    Jan 2 at 0:24





















0














The AuthGuard is not protecting the route. It needs to be declared within the Book module itself. See this question for more details.






share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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%2f53999776%2fwhen-are-routed-components-initialized%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Is the following fix your problem?



      const routes: Routes = [
    {
    path: 'find',
    component: FindBookPageComponent,
    },
    children:[
    {
    path: 'result',
    component: SearchResultComponent,
    },
    children:[
    {
    path: 'book/:id',
    component: ViewBookPageComponent ,
    }
    ]
    ]

    ]





    share|improve this answer


























    • This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

      – Baruch
      Jan 2 at 0:08











    • This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

      – Ole
      Jan 2 at 0:10











    • I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

      – Ole
      Jan 2 at 0:11











    • Hi, @Ole. See my update. Basically, use children routing.

      – Kevin
      Jan 2 at 0:23











    • Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

      – Ole
      Jan 2 at 0:24


















    0














    Is the following fix your problem?



      const routes: Routes = [
    {
    path: 'find',
    component: FindBookPageComponent,
    },
    children:[
    {
    path: 'result',
    component: SearchResultComponent,
    },
    children:[
    {
    path: 'book/:id',
    component: ViewBookPageComponent ,
    }
    ]
    ]

    ]





    share|improve this answer


























    • This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

      – Baruch
      Jan 2 at 0:08











    • This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

      – Ole
      Jan 2 at 0:10











    • I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

      – Ole
      Jan 2 at 0:11











    • Hi, @Ole. See my update. Basically, use children routing.

      – Kevin
      Jan 2 at 0:23











    • Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

      – Ole
      Jan 2 at 0:24
















    0












    0








    0







    Is the following fix your problem?



      const routes: Routes = [
    {
    path: 'find',
    component: FindBookPageComponent,
    },
    children:[
    {
    path: 'result',
    component: SearchResultComponent,
    },
    children:[
    {
    path: 'book/:id',
    component: ViewBookPageComponent ,
    }
    ]
    ]

    ]





    share|improve this answer















    Is the following fix your problem?



      const routes: Routes = [
    {
    path: 'find',
    component: FindBookPageComponent,
    },
    children:[
    {
    path: 'result',
    component: SearchResultComponent,
    },
    children:[
    {
    path: 'book/:id',
    component: ViewBookPageComponent ,
    }
    ]
    ]

    ]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 2 at 0:22

























    answered Jan 2 at 0:03









    KevinKevin

    170215




    170215













    • This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

      – Baruch
      Jan 2 at 0:08











    • This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

      – Ole
      Jan 2 at 0:10











    • I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

      – Ole
      Jan 2 at 0:11











    • Hi, @Ole. See my update. Basically, use children routing.

      – Kevin
      Jan 2 at 0:23











    • Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

      – Ole
      Jan 2 at 0:24





















    • This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

      – Baruch
      Jan 2 at 0:08











    • This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

      – Ole
      Jan 2 at 0:10











    • I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

      – Ole
      Jan 2 at 0:11











    • Hi, @Ole. See my update. Basically, use children routing.

      – Kevin
      Jan 2 at 0:23











    • Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

      – Ole
      Jan 2 at 0:24



















    This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

    – Baruch
    Jan 2 at 0:08





    This is the correct answer, wrong implementation. You should put find/:id under children property of find route and then have it /:id instead of find/:id.

    – Baruch
    Jan 2 at 0:08













    This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:10





    This is a full blown example with all the routes working. stackblitz.com/edit/akita-books-store

    – Ole
    Jan 2 at 0:10













    I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

    – Ole
    Jan 2 at 0:11





    I'm refactoring that example. The routes work and the :id is correct, as we wish to access a specific book at /books/:id and the routing used is relative to the books URL and its defined by the books module.

    – Ole
    Jan 2 at 0:11













    Hi, @Ole. See my update. Basically, use children routing.

    – Kevin
    Jan 2 at 0:23





    Hi, @Ole. See my update. Basically, use children routing.

    – Kevin
    Jan 2 at 0:23













    Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

    – Ole
    Jan 2 at 0:24







    Hi @Kevin. Happy new year! If you study the stackblitz.com/edit/akita-books-store example, you will see that the routing does actually work. What I'm wondering is why it does not work in my specific case, since in we have not navigated to books/:id yet.

    – Ole
    Jan 2 at 0:24















    0














    The AuthGuard is not protecting the route. It needs to be declared within the Book module itself. See this question for more details.






    share|improve this answer




























      0














      The AuthGuard is not protecting the route. It needs to be declared within the Book module itself. See this question for more details.






      share|improve this answer


























        0












        0








        0







        The AuthGuard is not protecting the route. It needs to be declared within the Book module itself. See this question for more details.






        share|improve this answer













        The AuthGuard is not protecting the route. It needs to be declared within the Book module itself. See this question for more details.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 17:59









        OleOle

        9,23784986




        9,23784986






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999776%2fwhen-are-routed-components-initialized%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith