Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 注入和使用Http Angular2_Javascript_Typescript_Angular - Fatal编程技术网

Javascript 注入和使用Http Angular2

Javascript 注入和使用Http Angular2,javascript,typescript,angular,Javascript,Typescript,Angular,我有一个服务类,它应该调用api并返回结果: import {Component, View} from 'angular2/angular2'; import { Inject} from 'angular2/di'; import {Http} from 'angular2/http'; var httpMap = new WeakMap<ScheduleService, Http>(); export class ScheduleService { meetings:

我有一个服务类,它应该调用api并返回结果:

import {Component, View} from 'angular2/angular2';
import { Inject} from 'angular2/di';
import {Http} from 'angular2/http';

var httpMap = new WeakMap<ScheduleService, Http>();
export class ScheduleService {
    meetings: Array<any>;
    http: any;

    constructor(@Inject(Http) http:Http){
        httpMap.set(this, http);
        this.http = http;

        //http.get('/api/sample')
        //   .map(response => response.json())
        //   .subscribe(data => {
        //      this.serverData = data;
        //      });
    }

    getMeetings(){
        var path = '/api/meetings/';
        return httpMap.get(this).get(path);
    }

}
从'angular2/angular2'导入{组件,视图};
从'angular2/di'导入{Inject};
从'angular2/Http'导入{Http};
var httpMap=new WeakMap();
导出类ScheduleService{
会议:阵列;
http:any;
构造函数(@Inject(Http)Http:Http){
set(this,http);
this.http=http;
//http.get('/api/sample')
//.map(response=>response.json())
//.订阅(数据=>{
//this.serverData=数据;
//      });
}
getMeetings(){
var path='/api/meetings/';
返回httpMap.get(this.get(path));
}
}
正在正确调用和注入服务类。我遇到的问题是,当我调用
getMeetings
方法时,它从未向
/api/meetings
发出请求。如果你们注意到构造器中有一个
get
/api/sample
的请求,如果我取消注释并运行程序,我会检查我的
网络
选项卡,我可以看到请求已经发出

看起来像
http.get(path)
不会发送请求,除非您调用
http.get(path.subscribe()即使您没有对响应执行任何操作

普朗克:


在Plunker中,打开控制台中的网络视图,然后单击带有订阅的不带订阅的按钮进行比较。

我对弱引用的了解非常贫乏,因此我无法回答为什么这种方式不起作用,所以,为什么不在
getMeetings()中使用
this.http
method?@EricMartinez本质上是一样的。我只是在看一些正在进行映射的博客,我只是觉得它看起来很脏,所以我尝试将它设置为对象的属性,以查看它是否工作,它是否确实可以。在调用
subscribe()
toPromise()
之前,Observables是惰性的,不做任何事情。