Nativescript 和 angular 2 Http 服务

Nativescript and angular 2 Http service

我正在通过 javascript 技术学习本机应用程序,并且我正在尝试使用仅可用的口袋妖怪 api 构建一个简单的应用程序。

我做了什么

我创建了一个简单的组件,它列出了一些 pokemons,来自 http 响应:

import {Component, OnInit} from '@angular/core';
import 'rxjs/Rx';
import {Http} from '@angular/http';

@Component({
  selector:'pokemon-list',
  templateUrl: 'components/pokemonList/pokemonList.html'
})
export default class PokemonList implements OnInit{

  pokemons: Array<any>;

  constructor(private http:Http){
    this.pokemons = [];
  }

  ngOnInit() {
    this.getRemote(); // I pass on here
  }

  getRemote(){
    this.http
      .get('https://pokeapi.co/api/v2/pokemon/') // Throws the weird error
      .map(res => res.json())
      .subscribe(res => {
        this.pokemons = res.results;
      });
  }
}

我的问题

当我启动我的应用程序时,我收到一个奇怪的错误

Error in app.component.html:4:4 caused by: null is not an object (evaluating '_angular_platformBrowser.__platform_browser_private__.getDOM().getCookie')

我注意到只有在我的 http 调用中设置了 getRemote 正文时才会发生此错误。此外,当我在我的宠物小精灵列表中设置默认宠物小精灵时,API 结果格式如 {name: 'Name', url: 'url},该应用程序正在运行口袋妖怪展示得很好。

如果我像下面这样删除代码,该应用程序将正常运行。似乎我在 Http 模块中遗漏了一些东西:

getRemote(){
    // App is running without the Http call
  }

注意:我正在使用 TS 2+ && 我在当前模块中设置了 Http 模块:

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import {HttpModule} from '@angular/http';
import { NativeScriptModule } from "nativescript-angular/platform";
import PokemonList from './components/pokemonList/pokemonList.component';
import PokemonItem from './components/pokemonItem/pokemonItem.component';
import { AppComponent } from "./app.component";

@NgModule({
    declarations: [
      AppComponent,
      PokemonList,
      PokemonItem
    ],
    bootstrap: [AppComponent],
    imports: [
      NativeScriptModule,
      HttpModule
    ],
    schemas: [NO_ERRORS_SCHEMA]
})
export class AppModule { }

知道我做错了什么吗?

感谢您的帮助

在 NgModule 而不是 HttpModule 中,您应该导入 NativeScript 包装器 NativeScriptHttpModule,如此处所示 https://github.com/NativeScript/nativescript-sdk-examples-ng/blob/master/app/http/http-examples.module.ts#L32

import { NativeScriptHttpModule } from "nativescript-angular/http";
...
@NgModule({
    imports: [
        NativeScriptHttpModule,
        ...

NativeScriptHttpModule is a NativeScript wrapper of Angular’s HttpModule, a module that declares all of Angular’s HTTP-based services...

需要在 NativeScript 中使用包装器,因为 NativeScript 不 "understand" DOM Angular

中使用的 Web 特定属性