无法读取未定义角度的属性“导航”

时间:2018-01-12 14:04:38

标签: angular

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");
        }
      }
    });
  }    

}

当用户点击CheckLogin 按钮时,它必须路由到登录组件,但在我这边显示错误。

我在stackoverflow中找到了我的问题的解决方案,但它不起作用,我不知道是什么问题。

2 个答案:

答案 0 :(得分:0)

试试这个

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});
}

答案 1 :(得分:0)

你需要添加=>这样你就可以获得对组件的引用 例如:

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

这里的答案对我来说是一个指针,它仍然像一个黑客,你需要做正确的事情 Angular2 injected Router is undefined