Angular 2服务无法解析所有参数

时间:2017-04-21 02:35:23

标签: json angular

您好我在最近两天尝试使用http读取本地json文件,不知道问题出在哪里

我的服务档案

import {Injectable} from '@angular/core';

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

export class AcquirerService{





  private _url : string = "agent_output1.json";



  constructor(private _http : Http){}

  getAcquirer(){

    return this._http.get(this._url).subscribe(
      (data) => console.log(data)

    )
     // .map((response:Response) => response.json());


  }

我的组件文件

import { Component ,OnInit} from '@angular/core';
import {AcquirerService} from '../services/acquirer.service'


@Component({

  selector : 'acquirer-message',
  templateUrl: '../templates/acquirer.component.html',
  providers :[AcquirerService]

})

export class AcquirerComponent implements  OnInit{

  acquirer_response ;
constructor(private _acquirerService : AcquirerService){


}

  ngOnInit(){

    this._acquirerService.getAcquirer();
     // .subscribe(resAcquirerData => this.acquirer_response = resAcquirerData);


}

}

我得到的错误是

metadata_resolver.js:972 Uncaught 
SyntaxError {__zone_symbol__error: Error: Can't resolve all parameters for AcquirerService: (?)

我的main.ts文件

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
import {HttpModule} from '@angular/http'


if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

我的app模块文件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule,JsonpModule } from '@angular/http';

import { AppComponent } from './components/app.component';
import {AcquirerComponent} from "./components/acquirer.component";
import {IssuerComponent} from "./components/issuer.component";

@NgModule({
  declarations: [
    AppComponent,AcquirerComponent,IssuerComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    JsonpModule
  ],
  providers: [],
  bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }

任何帮助将非常感谢我是棱角分明

2 个答案:

答案 0 :(得分:1)

将AcquirerService添加到提供者

@NgModule({
 declarations: [
 AppComponent,AcquirerComponent,IssuerComponent
 ],
imports: [
 BrowserModule,
 FormsModule,
 HttpModule,
 JsonpModule
],
providers: [AcquirerService],
bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }

在AcquirerService之前添加@Injectable()

@Injectable()
export class AcquirerService{ ... }

答案 1 :(得分:0)

在您的服务文件中,您导入了Injectable但未使用它。将@Injectable()放在服务名称之前,如此

@Injectable()
export class AcquirerService{
      //your code here

}