繁体   English   中英

Angular2上的http获取请求错误

[英]http get request error on angular2

我试图使用http方法获取请求,但我遇到一些问题来订阅响应。 这是我的服务:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/Rx';
import 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class ProfileService{
http: any;
url: String;
date: Date;

constructor(http:Http){
this.http = http;
this.date = new Date();
//this.baseurl = 'www.google.com';
}

get_heb_date():String{
var year = this.date.getFullYear();
var month = this.date.getMonth();
var day = this.date.getDay();
//var after_sunset = 1;
//var convert_from_gregorian_to_hebrew = 1;
return this.http.get('http://www.hebcal.com/converter/?fg=json&gy='+year+'&gm='+month+'&gd='+day+'&g2h=1')
.map(res => res.json());
 }}

这是我试图获取“ GET”请求的组件。

import { Component } from '@angular/core';
import { JewristService } from '../../app/services/jewrist.service';
import { NavController } from 'ionic-angular';
import { ProfileService } from '../../app/services/profile.service';
import 'rxjs/Rx';


@Component({
selector: 'page-profile',
templateUrl: 'profile.html',
providers: [ProfileService]
})
export class ProfilePage {
username: String;
loggedin: Boolean;
date: String;

constructor(public navCtrl: NavController, private _httpservice: ProfileService) {
this.username = 'null';
}
ngOnInit(){
console.log('Profile component is running..');
this._httpservice.get_heb_date().subscribe(
data => this.date = JSON.stringify(data)
 ); }}

我的IDE是Windows上的ATOM,这就是它指定的问题:类型'String'上不存在属性'subscribe'。

它应该是可观察的。

get_heb_date():Observable<string> // http return Observable<the_result_type>

问题出在main.ts内部

platformBrowserDynamic().bootstrapModule(AppModule);

未设置模块名称。 现在可以了。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM