【问题标题】:No requests during tns debugtns 调试期间没有请求
【发布时间】:2018-06-27 16:40:00
【问题描述】:

这是我在 nativescript 和一般移动开发环境中的第一个应用程序。我遇到了一些困难。我正在尝试使用 angular http 模块发出一些虚拟 http 请求,但由于某种原因,当我在 chrome 中调试应用程序时,似乎没有发出任何请求。

这是我的代码:

模板:

<Page>
    <StackLayout>
        <Button text="GET" (tap)="get()"></Button>
        <Button text="POST" (tap)="post()"></Button>
    </StackLayout>
</Page>

组件:

import { Component } from "@angular/core";
import { AuthService } from "../../shared/auth.service";
import { HttpClient } from "@angular/common/http";

@Component({
    selector: "register",
    moduleId: module.id,
    templateUrl: "./register.component.html"
})
export class RegisterComponent {

    constructor(private auth: AuthService, private http: HttpClient) {}

    get()
    {
        console.log('GET');
        this.http.get('https://httpbin.org/get');
    }

    post()
    {
        console.log('POST');
        this.http.post('https://httpbin.org/post', null);
    }
}

现在,当这些函数执行时,没有记录请求。 我在模拟器中运行,我可以很好地浏览它,所以如果有人对可能出现的问题有一些想法......

【问题讨论】:

    标签: android angular android-emulator nativescript angular2-nativescript


    【解决方案1】:

    您必须订阅 http 请求,否则它们只是被定义而不被调用,请尝试在 this.http.get('https://httpbin.org/get')this.http.post('https://httpbin.org/post', null) 之后添加以下内容:

    .subscribe(
        res => console.log(res),
        err => console.log(err),
        () => console.log("Done"),
    );
    

    编辑

    我本来没提,其实最好订阅返回http方法的函数,而不是订阅调用本身。根据经验,我发现可能会发生一些不寻常的行为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2016-06-18
      • 2021-09-15
      • 2015-03-21
      • 2011-06-13
      • 1970-01-01
      相关资源
      最近更新 更多