angular2无法解析AppComponent的所有参数:(?)

时间:2017-10-13 06:24:35

标签: angular angular2-services

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RestfulComponent } from '/app/rest/app.restful.component.ts';
import { HttpModule } from '@angular/http';

@NgModule({
  imports: [
    BrowserModule,
    HttpModule
  ],
  declarations: [
    RestfulComponent
  ],
  bootstrap: [ RestfulComponent ]
})
export class AppModule { }

app.restful.module.ts

import { Component } from '@angular/core';
import { RestfulService } from '/app/rest/restful/restfull.service.ts';

import {Http} from '@angular/http';
import { Observable } from 'rxjs';


@Component({
  selector: 'my-app',
  template: `
    <button (click)="onTestGet()">GET TEST</button><br>
    <p>Output: {{getData}}</p>
  `,
  providers: [RestfulService] 
})
export class RestfulComponent {

    getData: string;
    postData: string;
//
   constructor (private _http: Http){}

      onTestGet(){
          this._restfulService.getCurrentTime().subscribe(
          data => this.getData = JSON.stringify(data),
          error => alert(error),
          () => console.log("end")
              );
          }
}

app.restful.component.ts

import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Headers} from '@angular/http';
import 'rxjs/add/operator/map'

@Injectable()
export class RestfulService {
        constructor (private _http: Http){}

        getCurrentTime(){
            return this._http.get('http://date.jsontest.com').map(res => res.json());
        }
}

restful.service.ts

enter image description here

[enter image description here的test.html 2

我对角度的了解不多。如果您遗漏任何信息或文件,我在这里添加。

我将更改的代码放在他们生成以下错误的位置:

我该怎么办?

1 个答案:

答案 0 :(得分:2)

在构造函数方法中声明RestfulService:

(?4.)