Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'navigate' of undefined error

below is my code for navigation

         export class AppComponent {
           router:Router
            doSwipe(direction: string){

               this.router.navigate(['/article']);
            }
       }

I am getting Cannot read property 'navigate' of undefined error.pls help to fix it

like image 400
kamalav Avatar asked Nov 09 '17 08:11

kamalav


3 Answers

You need to inject the router, not just declare it as a property

constructor(private router: Router) {}
like image 81
Fabio Carpinato Avatar answered Sep 20 '22 00:09

Fabio Carpinato


In my case, I injected the router inside the constructor using the @Inject annotation.

constructor(@Inject(Router) private router: Router) {}
like image 39
khwilo Avatar answered Sep 19 '22 00:09

khwilo


You need to inject Router in constructor:

export class AppComponent {
    constructor(
        private router: Router
    ) {}

    doSwipe(direction: string) {

        this.router.navigate(['/article']);
    }
}
like image 25
Dhyey Avatar answered Sep 19 '22 00:09

Dhyey