0
import { Component, OnInit, Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import { GuessService } from './guess.service';
import 'rxjs/add/operator/map';
import {Router} from '@angular/router';

declare var jQuery:any;
declare var $ :any;

@Component({
  selector: 'app-guess',
  templateUrl: './guess.component.html',
  styleUrls: ['./guess.component.css']
})
@Injectable()
export class GuessComponent implements OnInit {

  constructor(private router: Router) {}

  ngOnInit() {


  }
  CheckLogin(name,pass)
  {

    $.ajax({
      headers:{  

   "Accept":"application/json",//depends on your api
    "Content-type":"application/x-www-form-urlencoded"//depends on your api
      },   url:"http://199.188.207.196:5555/user/login/",
      method:"post",
      data:{"email":name,"password":pass},
      success:function(response){

        if(response.success == true){
        console.log("ok");
        this.router.navigate(['/main']);
      }
        else{
          console.log("backend");
        }
      }
    });
  }    

}

When user click on CheckLogin button then it must route to login component, but in my side it show error.

I found solution of my question in stackoverflow but it's not working, i don't know what is the problem.

kappo
  • 272
  • 1
  • 3
  • 17

2 Answers2

1

you need to add => so that you can have the reference to the component.
e.g :

CheckLogin(name,pass) => {  
...  
} 

this answer here was a pointer for me, it is still like a hack, you need to do things properly.
Angular2 injected Router is undefined

Mashudu M
  • 31
  • 5
0

Try this

import { Component, OnInit, Injectable} from '@angular/core';
import {Router, ActivatedRoute} from '@angular/router';

export class GuessComponent implements OnInit {

constructor(private router: Router, private route: ActivatedRoute) {}

CheckLogin(name,pass)
{
  this.router.navigate(['/login'], {relativeTo: this.route});
}
justojas
  • 105
  • 9