0

我在打字稿中的 angular 2 中遇到问题,即浏览器没有进行 HTTP 调用。我在浏览器的网络部分看不到任何 HTTP 调用。当我在服务中没有任何 http 调用时,角度代码可以工作,并且当我使用 HTTP 时它会带来数据,它不会发出任何 http 请求

以下是我的代码。

<script src="~/lib/es6-shim.min.js"></script>
<script src="~/lib/system-polyfills.js"></script>
<script src="~/lib/shims_for_IE.js"></script>

<script src="~/lib/angular2-polyfills.js"></script>
<script src="~/lib/system.js"></script>
<script src="~/lib/Rx.js"></script>
<script src="~/lib/angular2.dev.js"></script>
<script src="~/lib/http.dev.js"></script>
<script src="~/lib/router.dev.js"></script>

服务 :

    import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';

@Injectable()
export class AuthService {

    private _authPath = 'http://api/Auth';
    constructor(private http: Http) { }
    getAuthSettings() {

        return new Promise((resolve, reject) => {

            return this.http.get(this._authPath).toPromise().then((val: Response) => resolve(val.text()));

        });
    }
    private handleError(error: Response) {
        // in a real world app, we may send the error to some remote logging infrastructure
        // instead of just logging it to the console
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }

}

主要模块

    import { Component } from 'angular2/core';
import {AuthService} from './auth.service';
import {OnInit} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';

@Component({

    selector: "main-app",
    template: "{{title}}",
    providers: [HTTP_PROVIDERS, AuthService]

})
export class AppComponent implements OnInit {
    public title: string = '';
    constructor(private _authService: AuthService) {
    }
    ngOnInit() {
        //this.title = "aaa";
        this._authService.getAuthSettings().then((val: string) => {
            this.title = val;

        });
        //this._authService.getAuthSettings().subscribe((val: string) => this.title = val)
    }

};
4

1 回答 1

1

我在浏览器的网络部分看不到任何 HTTP 调用。

可能有一些过滤器会阻止您看到它。为了确保更新代码如下:

 console.log('starting');
 this._authService.getAuthSettings()
     .then((val: string) => {
        this.title = val;
        console.log('success');
     },() => console.log('fail'));
 console.log('waiting');

并查看发生了哪些日志。希望它有所帮助。对不起,如果没有

于 2016-03-17T22:25:36.610 回答