0

I keep getting the following error when trying to navigate to "upload" page in Angular 2.

Cannot set property 'router' of undefined.

My Code:

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router';
export class GameLandingComponent implements OnInit{
    ngOnInit(){
        //some codes and logics...
        this.router= Router;
        this.router.navigateByUrl('/upload');
    }
}

Appreciate if someone could give me some help.TIA.

2 Answers 2

1

You need to inject router inside the constructor and you are missing constructor Try this -

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router';
export class GameLandingComponent implements OnInit{

    constructor(private router: Router){

    }

    ngOnInit(){

        this.router.navigateByUrl('/upload');
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

i think there is no need to make this.router = Router
Apologies! Was trying to post the entire code to make it easy to understand.
thanks for the quick response. Unfortunately, I still getting the same error after adding the constructor. :(
did you remove the part where you assign this.router
did you add routerModule in providers in app.module ?
|
0

it is not the way that you inject a class with inject it in the constructor

constructor(private router: Router){}
/// router now a private member of your class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.