【问题标题】:angular2 Can't resolve all parameters for AppComponent: (?)angular2 无法解析 AppComponent 的所有参数:(?)
【发布时间】:2017-10-13 14:41:05
【问题描述】:
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

[test.html2

我对 Angular 知之甚少。如果您缺少任何信息或文件,我会在这里补充。

我将把更改后的代码放在它们产生以下错误的地方:

我该怎么办??

【问题讨论】:

    标签: angular angular2-services


    【解决方案1】:

    在构造方法中声明 RestfulService :

    constructor(private _restfulService: RestfulService) { }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多