16

我创建了一个运行良好的简单 Hello World 应用程序。但是当我想添加一个“服务”只是一个简单的 Di 时,我得到了以下错误:

angular2.dev.js:23877 例外:无法解析“AppComponent”(?)的所有参数。确保所有参数都用 Inject 修饰或具有有效的类型注释,并且“AppComponent”用 Injectable 修饰。

angular2-polyfills.js:469 未处理的承诺拒绝:无法解析“AppComponent”(?)的所有参数。确保所有参数都用 Inject 修饰或具有有效的类型注释,并且“AppComponent”用 Injectable 修饰。; 区域:角度;任务:Promise.then;价值:

我有 3 个文件

引导.ts:

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './mainApp'
bootstrap(AppComponent);

mainApp.ts:

import {Component} from 'angular2/core';
import {CourseService} from './course.service';


@Component({
    selector: 'my-app',
    template: 'My First Angular 2 App',
    providers: [CourseService],
})
export class AppComponent {
    constructor(courseService: CourseService) {
    }
} 

course.service.ts:

export class CourseService {
   public getCourses(): string[] {
      let courses: string[] = ["Course 1", "Course 2", "Course 3"];
      return courses;
  }
}

当我从构造函数中删除 then 参数时,一切正常。

那是我的 HTML 文档的开头,我使用 angular 2 beta 13:

<script src="~/Scripts/es6-shim.min.js"></script>
<script src="~/Scripts/system-polyfills.js"></script>
<script src="~/Scripts/shims_for_IE.js"></script>
<script src="~/Scripts/angular2-polyfills.js"></script>
<script src="~/Scripts/system.js"></script>
<script src="~/Scripts/Rx.js"></script>
<script src="~/Scripts/angular2.dev.js"></script>

<!-- 2. Configure SystemJS -->
<script>
    System.config({
        packages: {
            'ScriptsApp/views': {
                defaultExtension: 'js'
            }
        }
    });
</script>
<script>
    System.import('ScriptsApp/views/boot').then(null, console.error.bind(console));
</script>

更新这些现在是我的固定 csproj TypeScript 设置

<TypeScriptTarget>ES5</TypeScriptTarget>
<TypeScriptJSXEmit>None</TypeScriptJSXEmit>
<TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
<TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
<TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
<TypeScriptRemoveComments>False</TypeScriptRemoveComments>
<TypeScriptOutFile />
<TypeScriptOutDir />
<TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
<TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
<TypeScriptSourceMap>True</TypeScriptSourceMap>
<TypeScriptMapRoot />
<TypeScriptSourceRoot />
<TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
<TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata>

在最新的 VS 2015 更新中,您还可以使用 tsconfig.json,然后忽略 TypeScript 的解决方案设置。我当前的 tsconfig.json 示例:

 {
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    //Kümmert sich um die Typings das diese entsprechend gefunden werden, keine Reference mehr in boot.js notwendig. Geht aber in VS nicht!
    //"lib": ["es6"],
    "types": [
      "node" //wird für RequireJs benötigt, damit z.b. der Typ gefunden werden kann
    ]
  },
  //Wird für den AwesomeTypeScriptLoad in WebPack benötigt!
   "awesomeTypescriptLoaderOptions": {
     "useWebpackText": true
   },
  "exclude": [
       "node_modules",
       "dist",
       "typings/main",
       "typings/index.d.ts"
     ],
     "compileOnSave": true
   }
4

3 回答 3

31

更新

在更新有问题的 OP 已经使用 Typescript 之后,我建议您emitDecoratorMetadata: truetsconfig.json. 这是必要的,因此 JavaScript 输出会在已转译的脚本文件中为装饰器创建元数据。


您需要添加@Inject以创建CourseService内部实例AppComponent

constructor(@Inject(CourseService) courseService: CourseService) {
}

如果您使用的是打字稿,则不需要在@Inject那里放置装饰器。Typescript 为您完成了这项工作。

于 2016-04-01T20:04:22.630 回答
9

我能够通过添加来解决这个问题:

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

在组件中并注释要注入的服务:

constructor(@Inject(CourseService) courseService: CourseService) {
于 2016-08-06T16:35:36.263 回答
0

就我而言,问题是由 @angular/forms 引起的。如果您在故障组件附近使用它,请开始检查您的 @angular/forms 并找到解决它的方法。

于 2017-05-31T14:52:45.650 回答