在Angular 6中,如果你在装饰器中使用了函数调用,会导致AOT编译错误。为了解决这个问题,你需要将函数调用移动到装饰器外部。
下面是一个示例代码,展示了如何解决“AOT编译错误:装饰器中不支持函数调用”错误:
// 错误示例
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
providers: [myService()] // 函数调用在装饰器中
})
export class ExampleComponent {
// ...
}
// 解决方法示例
const myServiceInstance = new myService(); // 将函数调用移到装饰器外部
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
providers: [myServiceInstance] // 在装饰器中使用实例
})
export class ExampleComponent {
// ...
}
在上面的示例中,我们将函数调用myService()
移动到装饰器外部,并创建了一个实例myServiceInstance
。然后,在装饰器中使用myServiceInstance
作为提供者。
通过这种方式,你可以避免“AOT编译错误:装饰器中不支持函数调用”错误,并正确地使用提供者。