Angular 2 HTTP“无法解析'AppService'的所有参数" [英] Angular 2 HTTP "Cannot resolve all parameters for 'AppService'"

查看:97
本文介绍了Angular 2 HTTP“无法解析'AppService'的所有参数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将http提供程序导入服务,但是出现以下错误:

I tried to import the http provider into a service, but I'm getting the following error:

无法解析'AppService'(?)的所有参数.确保所有参数都用Inject修饰或具有有效的类型注释,并且'AppService'用Injectable修饰.

Cannot resolve all parameters for 'AppService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AppService' is decorated with Injectable.

以下是一些代码段:

<script src="~/es6-shim/es6-shim.min.js"></script>
<script src="~/systemjs/dist/system-polyfills.js"></script>

<script src="~/angular2/bundles/angular2-polyfills.js"></script>
<script src="~/systemjs/dist/system.src.js"></script>
<script src="~/rxjs/bundles/Rx.js"></script>
<script src="~/angular2/bundles/angular2.dev.js"></script>
<script src="~/angular2/bundles/http.dev.js"></script>

<!-- 2. Configure SystemJS -->
<script>
    System.config({
        map: { 'rxjs': 'RCO/rxjs' },
        packages: {
            RCO: {
                format: 'register',
                defaultExtension: 'js'
            },
            'rxjs': {defaultExtension: 'js'}
        }
    });
  System.import('RCO/Areas/ViewOrganization/AngularTemplates/boot')
      .then(null, console.error.bind(console));

boot.ts

import {bootstrap} from 'angular2/platform/browser'
import {HTTP_PROVIDERS} from 'angular2/http'
import 'rxjs/add/operator/map'
import {AppComponent} from  'RCO/Areas/ViewOrganization/AngularTemplates/app.component'
import {AppService} from 'RCO/Areas/ViewOrganization/AngularTemplates/app.service'

bootstrap(AppComponent, [HTTP_PROVIDERS, AppService]);

app.service.ts

app.service.ts

import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';

@Injectable()
export class AppService {

    constructor(private http: Http) { }

    // Uses http.get() to load a single JSON file
    getTableData() {
        return this.http.get('...').map((res: Response) => res.json());
    }

}

我正在尝试调用服务器上的控制器,以最终将JSON文件加载到数据表中.很简单的东西,但是我加载Http模块的方式似乎是错误的.任何帮助将不胜感激.

I'm trying to call a controller on the server to eventually load a JSON file into a data table. Pretty straightforward stuff, but the way I'm loading the Http modules seems to be wrong. Any help will be much appreciated.

推荐答案

似乎您没有使用typescript编译器将文件转换为js,在这种情况下,您需要在注入任何依赖项时使用@Inject在组件构造函数中.

Seems like you are not using typescript compiler for transpiling files to js, In that case you need to have use @Inject while injecting any dependency inside a component constructor.

import {Injectable, Inject} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';

@Injectable()
export class AppService {
    constructor(@Inject(Http) private http: Http) { }
    // Uses http.get() to load a single JSON file
    getTableData() {
        return this.http.get('...').map((res: Response) => res.json());
    }
}

这篇关于Angular 2 HTTP“无法解析'AppService'的所有参数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆