我开始编写Angular 2并且遇到错误:
ts1206装饰器在这里无效
@Component({ // ts1206 decorators are not valid here
selector: 'my-app',
moduleId: module.id,
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
更新
我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
我能用它做什么?
答案 0 :(得分:100)
装饰器必须直接出现在导出的类之前,例如:
@Component({
...
})
export class someComponent{}
@Pipe
@Directive
@Injectable
和@NgModule
答案 1 :(得分:2)
当我在@NgModule
装饰器之后使用角度路由并定义了路由时,出现了此错误。
我们需要在@NgModule
装饰器之前定义路由或任何其他装饰器。
const appRoutes: Routes = [ // define this before @NgModule
{ path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{ path: 'home', component: HomeComponent },
];
@NgModule({ // This Decorator should be just before an exported class
declarations: [
AppComponent,
HeaderComponent,
HomeComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 2 :(得分:0)
界面道具{ ... }
@Component({ ... })
导出类someComponent {}