获取:“TypeError:this.get $ BrowserClient不是函数” [英] Getting: "TypeError: this.get$BrowserClient is not a function"

查看:198
本文介绍了获取:“TypeError:this.get $ BrowserClient不是函数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写AngularDart教程的修改版本。我基于开发人员指南/ HTTP客户端中的示例添加了一个服务器调用。而不是内存服务,我试图连接到同一台机器上的实际服务器。我已经在我的Rails应用程序中使用HTTPRequest在纯Dart工作。在我的新AngularDart独立客户端,我得到错误:

I'm coding a modified version of the AngularDart tutorial. I'm adding a server call based on the example in the Developer Guide/HTTP Client. Instead of the memory service, I'm trying to connect to an actual server on the same machine. I've gotten it to work in plain Dart using HTTPRequest in my Rails application. In my new AngularDart standalone client, I get the error:

TypeError: this.get$BrowserClient is not a function

我怀疑这可能是因为我从未在main.dart中创建内存服务器。

I suspect this may be happening because I never created the in memory server in main.dart. If so, what is the correct code to talk to a real server?

我的服务类:

import 'dart:async';
import 'dart:convert';

import 'package:angular2/core.dart';

import 'artist.dart';
import 'package:http/browser_client.dart';
import 'package:http/http.dart';

@Injectable()

class ArtistService {
  static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server
  final BrowserClient _http;
  ArtistService(this._http);

  Future<List<Artist>> getArtists() async {
    try {
      final response = await _http.get(_artistsUrl,
          headers: {'Accept': 'application/json'});
      final artists = _extractData(response)
          .map((value) => new Artist.fromJson(value))
          .toList();
      return artists;
    } catch (e) {
      throw _handleError(e);
    }
  }

...

我的main.dart是:

My main.dart is:

import 'package:angular2/platform/browser.dart';
import 'package:jazzcat/app_component.dart';

main() {
  bootstrap(AppComponent);
}

示例具有:

import 'package:angular2/core.dart';
import 'package:angular2/platform/browser.dart';
import 'package:http/browser_client.dart';

import 'package:server_communication/app_component.dart';
import "package:server_communication/hero_data.dart";

void main() {
  bootstrap(AppComponent, const [
    // in-memory web api provider
    const Provider(BrowserClient,
        useFactory: HttpClientBackendServiceFactory, deps: const [])
    // TODO: drop `deps` once fix lands for 
    // https://github.com/angular/angular/issues/5266
  ]);
}



计划B



我尝试从给定的链接:

Plan B

I tried this from the link given:

import 'package:http/http.dart' as http;


@Injectable()

class ArtistService {
  static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server

  Future<List<Artist>> getArtists() async {
    final response = await http.get(_artistsUrl, headers: {'Accept':    'application/json'});
    final artists = _extractData(response)
        .map((value) => new Artist.fromJson(value))
        .toList();
    return artists;
  }

我遇到错误:

EXCEPTION: Unsupported operation: IOClient isn't supported on this platform.



计划C



从另一个项目:

Plan C

I modified working code from another project:

import 'dart:html';

@Injectable()

class ArtistService {
  static const _artistsUrl = 'http://jazzcatold.loc/artists?page=1'; // URL to  RAILS server
  Map headers = {'Accept': 'application/json'};

  Future<List<Artist>> getArtists() async {
  final response = await HttpRequest.request(_artistsUrl, requestHeaders: headers);
  final artists = _extractData(response)
      .map((value) => new Artist.fromJson(value))
      .toList();
  return artists;
  }

当我在Chrome检查器上检查网络数据时,返回。我收到错误:

When I check the network data on the Chrome inspector, I see the JSON data being returned. I'm getting the error:

html_dart2js.dart:3352 EXCEPTION: NoSuchMethodError: method not found:   'get$body' (J.getInterceptor$x(...).get$body is not a function) 

可能不适合getArtists结构。

The code may not be a fit for the getArtists structure.

推荐答案

答案是上面的计划C的工作版本。

The answer is a working version of Plan C above.

import 'dart:async';
import 'dart:convert';
import 'package:angular2/core.dart';

import 'artist.dart';
import 'dart:html';


@Injectable()

class ArtistService {
  static const url = 'http://jazzcatold.loc/artists?page=1'; // URL to RAILS server
  Map headers = {'Accept': 'application/json'};

  Future<List<Artist>> getArtists() async {
    HttpRequest response = await HttpRequest.request(url, requestHeaders: headers);
    List data = JSON.decode(response.responseText);
    final artists = data
          .map((value) => new Artist.fromJson(value))
          .toList();
    return artists;
  }

我将_extractData方法集成到这一个。我还基于Günter的建议在其他地方更准确地编码它。上面的getBody错误在未显示的extractData方法中。

I rolled up the _extractData method into this one. I also coded it more precisely based on Günter's advice elsewhere. The getBody error above was in the extractData method that is not shown.

这篇关于获取:“TypeError:this.get $ BrowserClient不是函数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆