嗨,我正在尝试使用HTTP模块在Angular2中发出一个get请求。
在Typescript (1.5)中,所有内容都编译得很好,但是Chrome显示了以下错误,即in控制台:
EXCEPTION: Error during instantiation of EntryList!. ORIGINAL
EXCEPTION: TypeError: Cannot read property 'merge' of undefined
ORIGINAL STACKTRACE: TypeError: Cannot read property 'merge' of undefined
at mergeOptions (angular2.dev.js:27991)
at execute.Http.get (angular2.dev.js:28052)
at EntryService.getEntries (services.js:17)
at new EntryList (entry-list.js:20)Services.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
import { Http, Observable } from "angular2/angular2"
export class EntryService {
private _entries: Array<string>;
private _http : Http;
constructor() {
this._entries = ["test1", "test2", "test3"];
this._http = new Http;
}
get Entries() : Array<string> {
return this._entries;
}
getEntries()
{
console.log(this._http);
return this._http.get('http://localhost:53338/api/decisions')
.toRx()
.map((response) => {
response.json()
});
}
}entry-list.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
/// <reference path="../modules/services.ts" />
import { Component, View, NgFor } from "angular2/angular2"
import { EntryService } from "modules/services"
@Component({
selector: 'entry-list'
})
@View({
directives: [ NgFor ],
templateUrl: "../views/entry-list.html"
})
export class EntryList {
entries: Array<string>;
constructor(public service: EntryService) {
this.entries = this.service.Entries;
this.service.getEntries().subscribe((response) => {
console.log(response);
});
}
}app.ts
/// <reference path="typings/angular2/angular2.d.ts" />
/// <reference path="components/entry-list.ts" />
/// <reference path="modules/services.ts" />
import { Component, View, bootstrap, NgFor } from "angular2/angular2"
import { EntryList } from "components/entry-list"
import { EntryService } from "modules/services"
@Component({
selector : "app",
bindings: [ EntryService ]
})
@View({
directives: [ EntryList ],
templateUrl: "views/app.html"
})
class App {
constructor() {
console.log('hit');
}
}
bootstrap(App);index.html
<html>
<head>
<title>SCM</title>
<script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.88/traceur-runtime.js"></script>
<script src="https://jspm.io/system@0.16.js"></script>
<script src="https://code.angularjs.org/2.0.0-alpha.34/angular2.dev.js"></script>
</head>
<body>
<app></app>
<script>System.import('app')</script>
</body>
</html> 我确实查看了angular.io站点上的API文档,但没有发现出了什么问题。
更新
Services.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
import { Http, Observable, httpInjectables } from "angular2/angular2"
export class EntryService {
private _entries: Array<string>;
constructor(private http: Http) {
this._entries = ["test1", "test2", "test3"];
}
get Entries() : Array<string> {
return this._entries;
}
getEntries()
{
console.log(this.http);
return this.http.get('http://localhost:53338/api/decisions')
.toRx()
.map((response) => {
response.json()
});
}
}entry-list.ts
/// <reference path="../typings/angular2/angular2.d.ts" />
/// <reference path="../modules/services.ts" />
import { Component, View, NgFor, Http, httpInjectables } from "angular2/angular2"
import { EntryService } from "modules/services"
@Component({
selector: 'entry-list',
bindings : [ Http, httpInjectables ]
})
@View({
directives: [ NgFor ],
templateUrl: "../views/entry-list.html"
})
export class EntryList {
entries: Array<string>;
constructor(public service: EntryService) {
this.entries = this.service.Entries;
this.service.getEntries().subscribe((response) => {
console.log(response);
});
}
}app.ts
/// <reference path="typings/angular2/angular2.d.ts" />
/// <reference path="components/entry-list.ts" />
/// <reference path="modules/services.ts" />
import { Component, View, bootstrap, NgFor, Http, httpInjectables } from "angular2/angular2"
import { EntryList } from "components/entry-list"
import { EntryService } from "modules/services"
@Component({
selector : "app",
bindings: [ EntryService, Http, httpInjectables ]
})
@View({
directives: [ EntryList ],
templateUrl: "views/app.html"
})
class App {
constructor() {
console.log('hit');
}
}
bootstrap(App);不对index.html进行任何更改
编辑:我的services.ts文件更改以实现此工作:
/// <reference path="../typings/angular2/angular2.d.ts" />
import { Injector, Http, httpInjectables } from "angular2/angular2"
export class EntryService {
private _entries: Array<string>;
private http: Http;
constructor() {
this._entries = ["test1", "test2", "test3"];
var injector = Injector.resolveAndCreate([
httpInjectables,
Http
]);
this.http = injector.get(Http);
}
get Entries() : Array<string> {
return this._entries;
}
getEntries()
{
return this.http.get('http://localhost:53338/api/decisions')
.toRx()
.map(response => response.json());
}
}发布于 2015-08-19 14:53:16
您不应该自己实例化Http,而是将其注入:
public constructor(http : Http) {
this.http = http;
}文件上说:
Http作为一个可注入的类可用,它具有执行http请求的方法。
因此,如果您自己实例化它,我不知道结果是什么,但是由于一些内部元素是未定义的,它无法成功地执行get请求。
编辑:添加更多的资源。
@Injectable()
export class Http {
constructor(protected _backend: ConnectionBackend, protected _defaultOptions: RequestOptions) {}
/**
* Performs a request with `get` http method.
*/
get(url: string, options?: RequestOptionsArgs): EventEmitter {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethods.GET, url)));
}
}在这里,我从github添加了一部分Http类。您可以看到,_backend和_defaultOptions是在构造函数中给出的,而您没有给出,方法get()将根据options参数和构造函数中给出的_defaultOptions为请求构建选项,因为您没有提供这些选项,所以我相信它会在mergeOptions调用中崩溃,您可以在这里找到:
function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions {
var newOptions = defaultOpts;
if (isPresent(providedOpts)) {
// Hack so Dart can used named parameters
newOptions = newOptions.merge(new RequestOptions({
method: providedOpts.method,
url: providedOpts.url,
search: providedOpts.search,
headers: providedOpts.headers,
body: providedOpts.body,
mode: providedOpts.mode,
credentials: providedOpts.credentials,
cache: providedOpts.cache
}));
}
if (isPresent(method)) {
return newOptions.merge(new RequestOptions({method: method, url: url}));
} else {
return newOptions.merge(new RequestOptions({url: url}));
}
}在函数的最后一个if/else。有关更多信息,源文件位于here。
发布于 2015-10-14 08:29:45
您真的想只对CRUD/HTTP请求使用另一个库吗?**
我强烈推荐es6事实上的抓取。到目前为止,我认为它不仅比angular2 2/http更简单,而且现在和将来的版本也是如此。
**它也为safari、chrome、ie-edge和firefox打开了方框。
https://stackoverflow.com/questions/32096941
复制相似问题