kamalav
kamalav

Reputation: 1200

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

Upvotes: 7

Views: 32316

Answers (5)

Aviv Day
Aviv Day

Reputation: 438

A bit old, but in my case I had Fabio answer, but forgot to add @autoInject()

so you have to add

import { autoinject } from 'aurelia-framework';

@autoinject()
export class YourClass {
     
     constructor(private router: Router) {}

}

Upvotes: 0

khwilo
khwilo

Reputation: 521

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

constructor(@Inject(Router) private router: Router) {}

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222582

You need to inject the router inside the constructor,

constructor(
     private router: Router
){

}

Upvotes: 0

Dhyey
Dhyey

Reputation: 4335

You need to inject Router in constructor:

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

    doSwipe(direction: string) {

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

Upvotes: 1

Fabio Carpinato
Fabio Carpinato

Reputation: 1181

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

constructor(private router: Router) {}

Upvotes: 19

Related Questions