How to use routerlink in angular2?











up vote
1
down vote

favorite












I am trying to redirect from one page to another using routerlink and I had tried the following code,



I have the following route as my url



http://localhost:3000/#/app/dashboard


I want to redirect to this,when i click on this



  [routerLink]=" ['tables/basic'] "

http://localhost:3000/#/app/tables/basic


but it is going to



http://localhost:3000/#/app/dashboard/tables/basic


Here dashboard should be removed and how can I do that? Can anyone pls provide some help?



my routes.ts,



import { Routes } from '@angular/router';
import { ErrorComponent } from './error/error.component';


export const ROUTES: Routes = [{
path: '', redirectTo: 'app', pathMatch: 'full'
}, {
path: 'app', loadChildren: () => System.import('./layout/layout.module')
}, {
path: 'login', loadChildren: () => System.import('./login/login.module')
}, {
path: 'error', component: ErrorComponent
}, {
path: '**', component: ErrorComponent
}
];









share|improve this question




























    up vote
    1
    down vote

    favorite












    I am trying to redirect from one page to another using routerlink and I had tried the following code,



    I have the following route as my url



    http://localhost:3000/#/app/dashboard


    I want to redirect to this,when i click on this



      [routerLink]=" ['tables/basic'] "

    http://localhost:3000/#/app/tables/basic


    but it is going to



    http://localhost:3000/#/app/dashboard/tables/basic


    Here dashboard should be removed and how can I do that? Can anyone pls provide some help?



    my routes.ts,



    import { Routes } from '@angular/router';
    import { ErrorComponent } from './error/error.component';


    export const ROUTES: Routes = [{
    path: '', redirectTo: 'app', pathMatch: 'full'
    }, {
    path: 'app', loadChildren: () => System.import('./layout/layout.module')
    }, {
    path: 'login', loadChildren: () => System.import('./login/login.module')
    }, {
    path: 'error', component: ErrorComponent
    }, {
    path: '**', component: ErrorComponent
    }
    ];









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to redirect from one page to another using routerlink and I had tried the following code,



      I have the following route as my url



      http://localhost:3000/#/app/dashboard


      I want to redirect to this,when i click on this



        [routerLink]=" ['tables/basic'] "

      http://localhost:3000/#/app/tables/basic


      but it is going to



      http://localhost:3000/#/app/dashboard/tables/basic


      Here dashboard should be removed and how can I do that? Can anyone pls provide some help?



      my routes.ts,



      import { Routes } from '@angular/router';
      import { ErrorComponent } from './error/error.component';


      export const ROUTES: Routes = [{
      path: '', redirectTo: 'app', pathMatch: 'full'
      }, {
      path: 'app', loadChildren: () => System.import('./layout/layout.module')
      }, {
      path: 'login', loadChildren: () => System.import('./login/login.module')
      }, {
      path: 'error', component: ErrorComponent
      }, {
      path: '**', component: ErrorComponent
      }
      ];









      share|improve this question















      I am trying to redirect from one page to another using routerlink and I had tried the following code,



      I have the following route as my url



      http://localhost:3000/#/app/dashboard


      I want to redirect to this,when i click on this



        [routerLink]=" ['tables/basic'] "

      http://localhost:3000/#/app/tables/basic


      but it is going to



      http://localhost:3000/#/app/dashboard/tables/basic


      Here dashboard should be removed and how can I do that? Can anyone pls provide some help?



      my routes.ts,



      import { Routes } from '@angular/router';
      import { ErrorComponent } from './error/error.component';


      export const ROUTES: Routes = [{
      path: '', redirectTo: 'app', pathMatch: 'full'
      }, {
      path: 'app', loadChildren: () => System.import('./layout/layout.module')
      }, {
      path: 'login', loadChildren: () => System.import('./login/login.module')
      }, {
      path: 'error', component: ErrorComponent
      }, {
      path: '**', component: ErrorComponent
      }
      ];






      angular typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 '16 at 10:14









      Günter Zöchbauer

      303k60889838




      303k60889838










      asked Nov 7 '16 at 10:04









      Arnold

      40110




      40110
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Perhaps



          [routerLink]="['../tables/basic']"


          or



          [routerLink]="['/app/tables/basic']"


          To be sure you would need to provide the information what routes your application has configured and what component contains the [routerLink]






          share|improve this answer





















          • gunter, edited with my routes.ts
            – Arnold
            Nov 7 '16 at 10:13










          • Have you tried my suggestions?
            – Günter Zöchbauer
            Nov 7 '16 at 10:14










          • This one worked [routerLink]="['/app/tables/basic']"
            – Arnold
            Nov 7 '16 at 10:29


















          up vote
          0
          down vote













          if you want to route from button click you can easily use this, and also you can use it as a parameter parser through the components.firstly i will describe how to use it as a only for routing.



           <a [routerLink]="['/user/complain']">complain</a>


          from above example we can route the user/complain using complain tag



          now i am going to describe the second one(how to parse the parameters through the components)



          i will do it using the router params



          <a [routerLink]="['/user/complain', job.id]">complain</a>


          from that we can parse job.id to complain component.



          in complain component.ts file -



          export class ComplainComponent {

          id: number;
          private sub: any;

          constructor(private route: ActivatedRoute) { }

          ngOnInit() {
          this.sub = this.route.params.subscribe(params => {
          this.id = params['id']; // (+) converts string 'id' to a number


          });

          }

          }


          we can assign a variable for router parameter in this file.then you can easily whether is it worked or not by complain component.html file by



          <h3>{{job.id}}</h3>





          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',
            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%2f40462452%2fhow-to-use-routerlink-in-angular2%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Perhaps



            [routerLink]="['../tables/basic']"


            or



            [routerLink]="['/app/tables/basic']"


            To be sure you would need to provide the information what routes your application has configured and what component contains the [routerLink]






            share|improve this answer





















            • gunter, edited with my routes.ts
              – Arnold
              Nov 7 '16 at 10:13










            • Have you tried my suggestions?
              – Günter Zöchbauer
              Nov 7 '16 at 10:14










            • This one worked [routerLink]="['/app/tables/basic']"
              – Arnold
              Nov 7 '16 at 10:29















            up vote
            1
            down vote



            accepted










            Perhaps



            [routerLink]="['../tables/basic']"


            or



            [routerLink]="['/app/tables/basic']"


            To be sure you would need to provide the information what routes your application has configured and what component contains the [routerLink]






            share|improve this answer





















            • gunter, edited with my routes.ts
              – Arnold
              Nov 7 '16 at 10:13










            • Have you tried my suggestions?
              – Günter Zöchbauer
              Nov 7 '16 at 10:14










            • This one worked [routerLink]="['/app/tables/basic']"
              – Arnold
              Nov 7 '16 at 10:29













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Perhaps



            [routerLink]="['../tables/basic']"


            or



            [routerLink]="['/app/tables/basic']"


            To be sure you would need to provide the information what routes your application has configured and what component contains the [routerLink]






            share|improve this answer












            Perhaps



            [routerLink]="['../tables/basic']"


            or



            [routerLink]="['/app/tables/basic']"


            To be sure you would need to provide the information what routes your application has configured and what component contains the [routerLink]







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 7 '16 at 10:05









            Günter Zöchbauer

            303k60889838




            303k60889838












            • gunter, edited with my routes.ts
              – Arnold
              Nov 7 '16 at 10:13










            • Have you tried my suggestions?
              – Günter Zöchbauer
              Nov 7 '16 at 10:14










            • This one worked [routerLink]="['/app/tables/basic']"
              – Arnold
              Nov 7 '16 at 10:29


















            • gunter, edited with my routes.ts
              – Arnold
              Nov 7 '16 at 10:13










            • Have you tried my suggestions?
              – Günter Zöchbauer
              Nov 7 '16 at 10:14










            • This one worked [routerLink]="['/app/tables/basic']"
              – Arnold
              Nov 7 '16 at 10:29
















            gunter, edited with my routes.ts
            – Arnold
            Nov 7 '16 at 10:13




            gunter, edited with my routes.ts
            – Arnold
            Nov 7 '16 at 10:13












            Have you tried my suggestions?
            – Günter Zöchbauer
            Nov 7 '16 at 10:14




            Have you tried my suggestions?
            – Günter Zöchbauer
            Nov 7 '16 at 10:14












            This one worked [routerLink]="['/app/tables/basic']"
            – Arnold
            Nov 7 '16 at 10:29




            This one worked [routerLink]="['/app/tables/basic']"
            – Arnold
            Nov 7 '16 at 10:29












            up vote
            0
            down vote













            if you want to route from button click you can easily use this, and also you can use it as a parameter parser through the components.firstly i will describe how to use it as a only for routing.



             <a [routerLink]="['/user/complain']">complain</a>


            from above example we can route the user/complain using complain tag



            now i am going to describe the second one(how to parse the parameters through the components)



            i will do it using the router params



            <a [routerLink]="['/user/complain', job.id]">complain</a>


            from that we can parse job.id to complain component.



            in complain component.ts file -



            export class ComplainComponent {

            id: number;
            private sub: any;

            constructor(private route: ActivatedRoute) { }

            ngOnInit() {
            this.sub = this.route.params.subscribe(params => {
            this.id = params['id']; // (+) converts string 'id' to a number


            });

            }

            }


            we can assign a variable for router parameter in this file.then you can easily whether is it worked or not by complain component.html file by



            <h3>{{job.id}}</h3>





            share|improve this answer

























              up vote
              0
              down vote













              if you want to route from button click you can easily use this, and also you can use it as a parameter parser through the components.firstly i will describe how to use it as a only for routing.



               <a [routerLink]="['/user/complain']">complain</a>


              from above example we can route the user/complain using complain tag



              now i am going to describe the second one(how to parse the parameters through the components)



              i will do it using the router params



              <a [routerLink]="['/user/complain', job.id]">complain</a>


              from that we can parse job.id to complain component.



              in complain component.ts file -



              export class ComplainComponent {

              id: number;
              private sub: any;

              constructor(private route: ActivatedRoute) { }

              ngOnInit() {
              this.sub = this.route.params.subscribe(params => {
              this.id = params['id']; // (+) converts string 'id' to a number


              });

              }

              }


              we can assign a variable for router parameter in this file.then you can easily whether is it worked or not by complain component.html file by



              <h3>{{job.id}}</h3>





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                if you want to route from button click you can easily use this, and also you can use it as a parameter parser through the components.firstly i will describe how to use it as a only for routing.



                 <a [routerLink]="['/user/complain']">complain</a>


                from above example we can route the user/complain using complain tag



                now i am going to describe the second one(how to parse the parameters through the components)



                i will do it using the router params



                <a [routerLink]="['/user/complain', job.id]">complain</a>


                from that we can parse job.id to complain component.



                in complain component.ts file -



                export class ComplainComponent {

                id: number;
                private sub: any;

                constructor(private route: ActivatedRoute) { }

                ngOnInit() {
                this.sub = this.route.params.subscribe(params => {
                this.id = params['id']; // (+) converts string 'id' to a number


                });

                }

                }


                we can assign a variable for router parameter in this file.then you can easily whether is it worked or not by complain component.html file by



                <h3>{{job.id}}</h3>





                share|improve this answer












                if you want to route from button click you can easily use this, and also you can use it as a parameter parser through the components.firstly i will describe how to use it as a only for routing.



                 <a [routerLink]="['/user/complain']">complain</a>


                from above example we can route the user/complain using complain tag



                now i am going to describe the second one(how to parse the parameters through the components)



                i will do it using the router params



                <a [routerLink]="['/user/complain', job.id]">complain</a>


                from that we can parse job.id to complain component.



                in complain component.ts file -



                export class ComplainComponent {

                id: number;
                private sub: any;

                constructor(private route: ActivatedRoute) { }

                ngOnInit() {
                this.sub = this.route.params.subscribe(params => {
                this.id = params['id']; // (+) converts string 'id' to a number


                });

                }

                }


                we can assign a variable for router parameter in this file.then you can easily whether is it worked or not by complain component.html file by



                <h3>{{job.id}}</h3>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 4 hours ago









                malith vitha

                512




                512






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40462452%2fhow-to-use-routerlink-in-angular2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents