Promise

时间:2017-05-27 12:56:24

标签: angular angular-services

当尝试对我的后端进行API调用时,Angular应用程序崩溃了这些debuging信息:

          ERROR Error: Uncaught (in promise): Error: Loading chunk 1 failed.
onScriptComplete@http://127.0.0.1:4200/inline.bundle.js:92:33
wrapFn@http://127.0.0.1:4200/polyfills.bundle.js:6539:30
ZoneDelegate.prototype.invokeTask@http://127.0.0.1:4200/polyfills.bundle.js:5908:17
onInvokeTask@http://127.0.0.1:4200/vendor.bundle.js:4382:28
ZoneDelegate.prototype.invokeTask@http://127.0.0.1:4200/polyfills.bundle.js:5907:17
Zone.prototype.runTask@http://127.0.0.1:4200/polyfills.bundle.js:5675:28
ZoneTask/this.invoke@http://127.0.0.1:4200/polyfills.bundle.js:5970:28

Stacktrace:
resolvePromise@http://127.0.0.1:4200/polyfills.bundle.js:6253:31
resolvePromise@http://127.0.0.1:4200/polyfills.bundle.js:6224:17
scheduleResolveOrReject/<@http://127.0.0.1:4200/polyfills.bundle.js:6301:17
ZoneDelegate.prototype.invokeTask@http://127.0.0.1:4200/polyfills.bundle.js:5908:17
onInvokeTask@http://127.0.0.1:4200/vendor.bundle.js:4382:28
ZoneDelegate.prototype.invokeTask@http://127.0.0.1:4200/polyfills.bundle.js:5907:17
Zone.prototype.runTask@http://127.0.0.1:4200/polyfills.bundle.js:5675:28
drainMicroTaskQueue@http://127.0.0.1:4200/polyfills.bundle.js:6068:25
ZoneTask/this.invoke@http://127.0.0.1:4200/polyfills.bundle.js:5974:25

我想知道如何获得更详细的调试信息?我的consol.log语句不起作用这是正常的吗?

这些错误的根源是什么?它看起来在我的矿山服务承诺中有一些东西,但我没有发现错误。

user.service.ts

import { Injectable } from '@angular/core';
import {Headers, Http} from "@angular/http";

import 'rxjs/add/operator/toPromise';


export class UserLogin{
    username: string;
    password: string;
}
export class LoginToken{
    token: string
}

@Injectable()
export class UserService {
    private loginUrl = 'http://127.0.0.1:8000/api/auth/login/';  // URL to web api
    private headers = new Headers({'Content-Type': 'application/json'});
    private UserLogin: UserLogin;

    constructor(private http: Http) { }

    public login(username: string, password: string): Promise<LoginToken> {
        console.log(username);
        this.UserLogin.username = username;
        this.UserLogin.password = password;
        console.log(this.UserLogin);
        return this.http
                .post(this.loginUrl,JSON.stringify(this.UserLogin), {headers: this.headers})
                .toPromise()
                .then(response => response.json().data as LoginToken)
                .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

}

login.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import {UserService} from "../shared/services/user.service";

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss']
})
export class LoginComponent{
    constructor(
        public router: Router,
        private UserService: UserService,
    ) { }


    username: string = "";
    password: string = "";
    token: string = "";


    login(){
        console.log(this.username);
        console.log(this.password);
        this.UserService.login(this.username, this.password)
            .then(token => this.token = token.token);

        localStorage.setItem('loginToken', this.token);
        localStorage.setItem('isLoggedin', 'true');
    }

}

0 个答案:

没有答案