angular routing - navigate to same component with additional params without destroying the component












0















I have a routing setting like this



{
path: '',
component: BaseCmp,
children: [
{ path: 'list', component: ListCmp },
{
path: 'list/:itemId',
component: ListItemCmp,
},
{
path: 'list/:itemId/:subNavItemId',
component: ListItemCmp,
}
]
}


I'm selecting an Item from the ListCmp and get navigated to for example 'list/123'



In the ListCmp Constructor/OnInit I'm fetching some data with the :itemId and it returns a list of SubNavItems, from which the id of the first SubNavItem should be added as params (for routerLinkActive to highlight and other fetching purposes).



constructor(route:ActivatedRoute, http:HttpClient, router:Router){
let id = route.params.itemId
http.get(.../id).subscribe(list => {
// add id of first item of list to params
this.router.navigate(['list', id, list[0].id, {replaceUrl:true}]
})
}


This will cause the destruction of the current ListItemCmp and instantiate a new ListItemCmp. How can I prevent that? I've tried the location.go() method but that won't trigger the routerLinkActive










share|improve this question


















  • 1





    'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

    – trichetriche
    Nov 21 '18 at 11:13











  • thanks, I did just that, if you post a reply I can check it off

    – Han Che
    Nov 21 '18 at 14:07
















0















I have a routing setting like this



{
path: '',
component: BaseCmp,
children: [
{ path: 'list', component: ListCmp },
{
path: 'list/:itemId',
component: ListItemCmp,
},
{
path: 'list/:itemId/:subNavItemId',
component: ListItemCmp,
}
]
}


I'm selecting an Item from the ListCmp and get navigated to for example 'list/123'



In the ListCmp Constructor/OnInit I'm fetching some data with the :itemId and it returns a list of SubNavItems, from which the id of the first SubNavItem should be added as params (for routerLinkActive to highlight and other fetching purposes).



constructor(route:ActivatedRoute, http:HttpClient, router:Router){
let id = route.params.itemId
http.get(.../id).subscribe(list => {
// add id of first item of list to params
this.router.navigate(['list', id, list[0].id, {replaceUrl:true}]
})
}


This will cause the destruction of the current ListItemCmp and instantiate a new ListItemCmp. How can I prevent that? I've tried the location.go() method but that won't trigger the routerLinkActive










share|improve this question


















  • 1





    'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

    – trichetriche
    Nov 21 '18 at 11:13











  • thanks, I did just that, if you post a reply I can check it off

    – Han Che
    Nov 21 '18 at 14:07














0












0








0








I have a routing setting like this



{
path: '',
component: BaseCmp,
children: [
{ path: 'list', component: ListCmp },
{
path: 'list/:itemId',
component: ListItemCmp,
},
{
path: 'list/:itemId/:subNavItemId',
component: ListItemCmp,
}
]
}


I'm selecting an Item from the ListCmp and get navigated to for example 'list/123'



In the ListCmp Constructor/OnInit I'm fetching some data with the :itemId and it returns a list of SubNavItems, from which the id of the first SubNavItem should be added as params (for routerLinkActive to highlight and other fetching purposes).



constructor(route:ActivatedRoute, http:HttpClient, router:Router){
let id = route.params.itemId
http.get(.../id).subscribe(list => {
// add id of first item of list to params
this.router.navigate(['list', id, list[0].id, {replaceUrl:true}]
})
}


This will cause the destruction of the current ListItemCmp and instantiate a new ListItemCmp. How can I prevent that? I've tried the location.go() method but that won't trigger the routerLinkActive










share|improve this question














I have a routing setting like this



{
path: '',
component: BaseCmp,
children: [
{ path: 'list', component: ListCmp },
{
path: 'list/:itemId',
component: ListItemCmp,
},
{
path: 'list/:itemId/:subNavItemId',
component: ListItemCmp,
}
]
}


I'm selecting an Item from the ListCmp and get navigated to for example 'list/123'



In the ListCmp Constructor/OnInit I'm fetching some data with the :itemId and it returns a list of SubNavItems, from which the id of the first SubNavItem should be added as params (for routerLinkActive to highlight and other fetching purposes).



constructor(route:ActivatedRoute, http:HttpClient, router:Router){
let id = route.params.itemId
http.get(.../id).subscribe(list => {
// add id of first item of list to params
this.router.navigate(['list', id, list[0].id, {replaceUrl:true}]
})
}


This will cause the destruction of the current ListItemCmp and instantiate a new ListItemCmp. How can I prevent that? I've tried the location.go() method but that won't trigger the routerLinkActive







angular angular-routing angular-router angular-routerlink






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 10:56









Han CheHan Che

2,37263166




2,37263166








  • 1





    'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

    – trichetriche
    Nov 21 '18 at 11:13











  • thanks, I did just that, if you post a reply I can check it off

    – Han Che
    Nov 21 '18 at 14:07














  • 1





    'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

    – trichetriche
    Nov 21 '18 at 11:13











  • thanks, I did just that, if you post a reply I can check it off

    – Han Che
    Nov 21 '18 at 14:07








1




1





'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

– trichetriche
Nov 21 '18 at 11:13





'list/:itemId/:subNavItemId' should be a child of 'list/:itemId' : because of that, your routing creates two instances of a same class, but has to destroy them to route away to the URL. Make it a child of the first route, then your component should not be destroyed anymore.

– trichetriche
Nov 21 '18 at 11:13













thanks, I did just that, if you post a reply I can check it off

– Han Che
Nov 21 '18 at 14:07





thanks, I did just that, if you post a reply I can check it off

– Han Che
Nov 21 '18 at 14:07












2 Answers
2






active

oldest

votes


















0














Following my comment, your error was coming from the fact that your routes were siblings instead of parents : by routing away from the first sibling, you were destroying it to create the other sibling.



With parent routes, the parent will stay (because it contains a router outlet and can't disappear), while the child will be appended to the parent, meaning the parent doesn't get destroyed.






share|improve this answer































    0














    you can use query params instead of hard routing



        constructor(route: ActivatedRoute) {
    let id = route.params.itemId
    http.get(.../id).subscribe(list => {
    // add id of first item of list to params
    this.router.navigate(['list', id], { queryParams: { subNavItemId: list[0].id } })
    })
    this.activatedRoute.queryParamMap.subscribe(query => {
    if (query['params']['subNavItemId']) {
    // perform some action
    }
    });
    }


    In your router you can now remove "list/:itemId/:subNavItemId"



    {
    path: '',
    component: BaseCmp,
    children: [
    { path: 'list', component: ListCmp },
    {
    path: 'list/:itemId',
    component: ListItemCmp,
    },
    // you can remove below route
    // {
    // path: 'list/:itemId/:subNavItemId',
    // component: ListItemCmp,
    // }
    ]
    }





    share|improve this answer


























    • That doesn't answer the question, that provides a workaround for the issue.

      – trichetriche
      Nov 21 '18 at 11:11











    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%2f53410601%2fangular-routing-navigate-to-same-component-with-additional-params-without-dest%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














    Following my comment, your error was coming from the fact that your routes were siblings instead of parents : by routing away from the first sibling, you were destroying it to create the other sibling.



    With parent routes, the parent will stay (because it contains a router outlet and can't disappear), while the child will be appended to the parent, meaning the parent doesn't get destroyed.






    share|improve this answer




























      0














      Following my comment, your error was coming from the fact that your routes were siblings instead of parents : by routing away from the first sibling, you were destroying it to create the other sibling.



      With parent routes, the parent will stay (because it contains a router outlet and can't disappear), while the child will be appended to the parent, meaning the parent doesn't get destroyed.






      share|improve this answer


























        0












        0








        0







        Following my comment, your error was coming from the fact that your routes were siblings instead of parents : by routing away from the first sibling, you were destroying it to create the other sibling.



        With parent routes, the parent will stay (because it contains a router outlet and can't disappear), while the child will be appended to the parent, meaning the parent doesn't get destroyed.






        share|improve this answer













        Following my comment, your error was coming from the fact that your routes were siblings instead of parents : by routing away from the first sibling, you were destroying it to create the other sibling.



        With parent routes, the parent will stay (because it contains a router outlet and can't disappear), while the child will be appended to the parent, meaning the parent doesn't get destroyed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 14:10









        trichetrichetrichetriche

        26.7k42255




        26.7k42255

























            0














            you can use query params instead of hard routing



                constructor(route: ActivatedRoute) {
            let id = route.params.itemId
            http.get(.../id).subscribe(list => {
            // add id of first item of list to params
            this.router.navigate(['list', id], { queryParams: { subNavItemId: list[0].id } })
            })
            this.activatedRoute.queryParamMap.subscribe(query => {
            if (query['params']['subNavItemId']) {
            // perform some action
            }
            });
            }


            In your router you can now remove "list/:itemId/:subNavItemId"



            {
            path: '',
            component: BaseCmp,
            children: [
            { path: 'list', component: ListCmp },
            {
            path: 'list/:itemId',
            component: ListItemCmp,
            },
            // you can remove below route
            // {
            // path: 'list/:itemId/:subNavItemId',
            // component: ListItemCmp,
            // }
            ]
            }





            share|improve this answer


























            • That doesn't answer the question, that provides a workaround for the issue.

              – trichetriche
              Nov 21 '18 at 11:11
















            0














            you can use query params instead of hard routing



                constructor(route: ActivatedRoute) {
            let id = route.params.itemId
            http.get(.../id).subscribe(list => {
            // add id of first item of list to params
            this.router.navigate(['list', id], { queryParams: { subNavItemId: list[0].id } })
            })
            this.activatedRoute.queryParamMap.subscribe(query => {
            if (query['params']['subNavItemId']) {
            // perform some action
            }
            });
            }


            In your router you can now remove "list/:itemId/:subNavItemId"



            {
            path: '',
            component: BaseCmp,
            children: [
            { path: 'list', component: ListCmp },
            {
            path: 'list/:itemId',
            component: ListItemCmp,
            },
            // you can remove below route
            // {
            // path: 'list/:itemId/:subNavItemId',
            // component: ListItemCmp,
            // }
            ]
            }





            share|improve this answer


























            • That doesn't answer the question, that provides a workaround for the issue.

              – trichetriche
              Nov 21 '18 at 11:11














            0












            0








            0







            you can use query params instead of hard routing



                constructor(route: ActivatedRoute) {
            let id = route.params.itemId
            http.get(.../id).subscribe(list => {
            // add id of first item of list to params
            this.router.navigate(['list', id], { queryParams: { subNavItemId: list[0].id } })
            })
            this.activatedRoute.queryParamMap.subscribe(query => {
            if (query['params']['subNavItemId']) {
            // perform some action
            }
            });
            }


            In your router you can now remove "list/:itemId/:subNavItemId"



            {
            path: '',
            component: BaseCmp,
            children: [
            { path: 'list', component: ListCmp },
            {
            path: 'list/:itemId',
            component: ListItemCmp,
            },
            // you can remove below route
            // {
            // path: 'list/:itemId/:subNavItemId',
            // component: ListItemCmp,
            // }
            ]
            }





            share|improve this answer















            you can use query params instead of hard routing



                constructor(route: ActivatedRoute) {
            let id = route.params.itemId
            http.get(.../id).subscribe(list => {
            // add id of first item of list to params
            this.router.navigate(['list', id], { queryParams: { subNavItemId: list[0].id } })
            })
            this.activatedRoute.queryParamMap.subscribe(query => {
            if (query['params']['subNavItemId']) {
            // perform some action
            }
            });
            }


            In your router you can now remove "list/:itemId/:subNavItemId"



            {
            path: '',
            component: BaseCmp,
            children: [
            { path: 'list', component: ListCmp },
            {
            path: 'list/:itemId',
            component: ListItemCmp,
            },
            // you can remove below route
            // {
            // path: 'list/:itemId/:subNavItemId',
            // component: ListItemCmp,
            // }
            ]
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 11:13

























            answered Nov 21 '18 at 11:10









            Thomsheer AhamedThomsheer Ahamed

            686




            686













            • That doesn't answer the question, that provides a workaround for the issue.

              – trichetriche
              Nov 21 '18 at 11:11



















            • That doesn't answer the question, that provides a workaround for the issue.

              – trichetriche
              Nov 21 '18 at 11:11

















            That doesn't answer the question, that provides a workaround for the issue.

            – trichetriche
            Nov 21 '18 at 11:11





            That doesn't answer the question, that provides a workaround for the issue.

            – trichetriche
            Nov 21 '18 at 11:11


















            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%2f53410601%2fangular-routing-navigate-to-same-component-with-additional-params-without-dest%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

            Npm cannot find a required file even through it is in the searched directory