Angular : Manual redirect to route

后端 未结 8 1179
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 19:50

I just recently started using angular 4 instead of angular.js 1.

I have followed the heroes tutorial to learn about the fundamentals of angular 4 and I am currently

8条回答
  •  感动是毒
    2020-12-12 20:56

    Angular routing : Manual navigation

    First you need to import the angular router :

    import {Router} from "@angular/router"
    

    Then inject it in your component constructor :

    constructor(private router: Router) { }
    

    And finally call the .navigate method anywhere you need to "redirect" :

    this.router.navigate(['/your-path'])
    

    You can also put some parameters on your route, like user/5 :

    this.router.navigate(['/user', 5])
    

    Documentation: Angular official documentaiton

提交回复
热议问题