ts1206装饰器在这里无效,Angular 2

Eli*_*iel 47 typescript angular

我开始编写Angular 2,我遇到了一个错误:

ts1206装饰器在这里无效

@Component({   //  ts1206 decorators are not valid here
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
Run Code Online (Sandbox Code Playgroud)

更新:

我的tsconfig.json:

 {
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Jar*_*ser 113

装饰器必须直接出现在导出的类之前,例如:

@Component({
    ...
})
export class someComponent{}
Run Code Online (Sandbox Code Playgroud)

这对于@Pipe @Directive @Injectable@NgModule

  • 顺序很重要。我在 Injectable 之后有一个导出接口并收到此错误。将导出类移动到紧邻 Injectable 后面以及之后的接口解决了问题。 (2认同)