【发布时间】:2017-03-11 13:38:48
【问题描述】:
我正在实施以下教程以获得一些 Angular 基础知识: http://jasonwatmore.com/post/2016/08/16/angular-2-jwt-authentication-example-tutorial
此外,我实际上想使用“angular2-jwt”库。但是,如果本地没有保存 JWT,我使用 authHttp 发送的每个 GET 或 POST 请求都会失败,控制台中只会出现一个空错误。
“错误:login.component.html 中的错误由以下原因引起:”
没有“没有 JWT 存在或已过期”错误文本,什么都没有。
顺便说一句,标准的 Http Class 工作正常。 “tokenNotExpired()”方法也是如此。
还尝试添加
'js-base64':'npm:js-base64/base64.js',
'buffer':'@empty'
映射到我的systemsjs.config.js的信息,还是没有结果。
知道我做错了什么吗? :(
Angular 2.1.0 + angular2-jwt 0.1.25
app.module.ts
import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import {AppComponent} from "./app.component";
import {AppRoutingModule} from "./app-routing.module";
import {LoginComponent} from "./login/login.component";
import {MainComponent} from "./main/main.component";
import {AuthService} from "./services/auth.service";
import {AUTH_PROVIDERS} from "angular2-jwt";
@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
HttpModule
],
declarations: [
AppComponent,
LoginComponent,
MainComponent
],
providers: [
AuthService,
AUTH_PROVIDERS
],
bootstrap: [AppComponent]
})
export class AppModule
{
}
login.component.ts
import { Component } from '@angular/core';
import { AuthService } from "../services/auth.service";
@Component({
moduleId: module.id,
templateUrl: 'login.component.html'
})
export class LoginComponent {
user: any = {
login: 'defaultLogin',
password: 'defaultPassword'
};
errorMsg = '';
constructor(
private authService: AuthService) { }
login() {
this.authService.login(this.user.name, this.user.password)
.subscribe(result => {
});
}
}
auth.service.ts
import {Injectable} from "@angular/core";
import {Response} from "@angular/http";
import "rxjs/add/operator/map";
import "rxjs/add/operator/toPromise";
import {AuthHttp} from "angular2-jwt";
@Injectable()
export class AuthService
{
constructor(private authHttp: AuthHttp)
{
}
login(username: string, password: string)
{
return this.authHttp.post('/api/login', JSON.stringify({username: username, password: password}))
.map((response: Response) =>{
return false;
});
}
}
【问题讨论】:
-
我得到了类似的东西——错误被吞没了,我得到了一个非常奇怪的异常,即 Observables 没有处理拒绝。在 JWT 库中进行一些调试后,我发现这是被抛出的:obs.error(new AuthHttpError('No JWT present or has expired'));