Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to navigate one route to another in angular 2

I make a two different component of angular 2 .I am learning routing from this link https://angular.io/docs/ts/latest/tutorial/toh-pt5.html

I make two component .In first component I have one button I want to move to move to second component

here is my code https://plnkr.co/edit/GBOI9avZaPGaxaLQbtak

I define routes like that

    const routes =[
      {
        path: 'ft',
        component: First
      },
      {
        path: 'sd',
        component: Second
      }
    ]

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App,First ,Second]
}) 

I am using <router-outlet>

but I am not able to move one component to another

like image 255
user944513 Avatar asked Dec 15 '22 01:12

user944513


1 Answers

There you go :

@Component({
  selector: 'first',
  template: `
    <div>
      <button (click)="moveToSecond()">move to secon comp</button>
    </div>
  `,
})
export class First {
  name:string;
  constructor(private router:Router) {
  }

  moveToSecond(){
    this.router.navigate(['/sd']);
  }
}

https://plnkr.co/edit/wIuaffLskQd8GJuqLlY6?p=preview

You had heaps of errors :D

Anyway , in order to navigate to another route , you'd need to use router

like image 114
Milad Avatar answered Dec 28 '22 09:12

Milad