如何通过NativeScript-Angular课程2步骤10进行学习?

时间:2019-04-03 11:18:02

标签: typescript nativescript nativescript-angular

我想为NativeScript和Angular做Playground教程。由于出现编译错误,我被困在第2课的第10步中,因为该类不知道登录功能的与http相关的段。

import { Injectable } from "@angular/core";

import { User } from "./user.model";

@Injectable()
export class UserService {
register(user: User) {
alert("About to register: " + user.email);
}

login(user: User) {
    return this.http.post(
        Config.apiUrl + "user/" + Config.appKey + "/login",
        JSON.stringify({
            username: user.email,
            password: user.password
        }),
        { headers: this.getCommonHeaders() }
    ).pipe(
        map(response => response.json()),
        tap(data => {
            Config.token = data._kmd.authtoken
        }),
        catchError(this.handleErrors)
    );
}
}

我认为是什么,尚不重要,这就是与HTTP相关的功能不起作用的原因。 如果我错过了一些东西,但是找不到任何东西,我在教程代码中进行了搜索。

如果有人可以告诉我如何继续本教程,或者告诉我要找到所需的代码段,我会很好。:)

1 个答案:

答案 0 :(得分:0)

Check your setup

  • 确认已在模块中添加HttpClient的导入
    import { HttpClientModule } from '@angular/common/http';

    @NgModule({
      imports: [
        BrowserModule,
        // import HttpClientModule after BrowserModule.
        HttpClientModule,
      ],
      declarations: [
        AppComponent,
      ],
      bootstrap: [ AppComponent ]
    })

  • 检查是否已将http对象添加到类构造函数中
constructor(private http: HttpClient) { }

然后您可以从类中的任何函数中引用http

doSomePosting() {
    // this.http.post(postUrl, postData, postOptions);
}