【问题标题】:http get request error on angular2angular2上的http获取请求错误
【发布时间】:2016-12-21 16:36:14
【问题描述】:

我正在尝试使用 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,这就是它指定的问题: 类型“字符串”上不存在属性“订阅”。

【问题讨论】:

    标签: javascript angular typescript ionic2


    【解决方案1】:

    它应该是可观察的。

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

    【讨论】:

    • 找不到名称 Observable
    • 我添加了 import {Observable} from 'rxjs/Rx
    • 收到另一个错误:找不到模块 angular2/router
    • 在你的组件中某处你使用路由?然后你需要在你的应用模块中包含路由器
    • 我不使用任何路线。
    【解决方案2】:

    问题出在 main.ts 内部

    platformBrowserDynamic().bootstrapModule(AppModule);
    

    未设置模块名称。 现在它起作用了。 谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-07
      • 2017-12-22
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2017-12-24
      • 2016-10-14
      相关资源
      最近更新 更多