Angular 角形2罐';调用服务后,无法在subscribe方法中执行操作
我有一个服务来验证我的用户,但我不知道如何处理我收到的数据。我不想根据用户的角色将其重定向到正确的页面,并在页面中显示其姓名。 我尝试了不同的方法,但当我调用路由器或函数时,它永远找不到。看起来好像不在同一个背景下。我该怎么做 这是我的登录组件:Angular 角形2罐';调用服务后,无法在subscribe方法中执行操作,angular,typescript,Angular,Typescript,我有一个服务来验证我的用户,但我不知道如何处理我收到的数据。我不想根据用户的角色将其重定向到正确的页面,并在页面中显示其姓名。 我尝试了不同的方法,但当我调用路由器或函数时,它永远找不到。看起来好像不在同一个背景下。我该怎么做 这是我的登录组件: import { Component, OnInit } from '@angular/core'; import {User} from "../models/user"; import {AuthenticationServic
import { Component, OnInit } from '@angular/core';
import {User} from "../models/user";
import {AuthenticationService} from "../services/authentication.service";
import {Router} from "@angular/router";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
providers: [AuthenticationService]
})
export class LoginComponent implements OnInit {
user:User = new User();
result: any;
constructor(private authenticationService:AuthenticationService,
private router:Router) {
}
loginAction(){
this.authenticationService.login(this.user)
.subscribe(function(result){
this.result = result;
console.log(this.result); // result : {token: "ABCDE....", "email":"test@test.com", "username": "Jack James", role:"ADMIN"}
this.doSth(); //Never called I supposed it's not the same context
this.router.navigate(['home']); // router is not find
// I wan't to do actions to redirect to another page and display the user currently logged in
},
err => {
// Log errors if any
console.log(err);
});
}
doSth(){
console.log("Do something");
}
ngOnInit() {
}
}
您的问题如下:
.subscribe(函数(结果){
使用该函数()
您必须使用箭头语法()=>
像这样:.subscribe((结果)=>{
就是这样,现在您可以使用这个。路由器…
:)什么是“路由器未找到”意思?路由器未定义?路由器找不到路由?没关系,您可以看到答案