我目前的角度项目在角度支持AOT功能之前就开始了。现在我正试图让它发挥作用。我收到以下错误,我不知道这意味着什么,我可以开始调试问题。有谁知道为什么会出现这个错误?
依赖
"@angular/animations": "^4.4.4",
"@angular/common": "^4.4.4",
"@angular/compiler": "^4.4.4",
"@angular/core": "^4.4.4",
"@angular/forms": "^4.4.4",
"@angular/platform-browser": "^4.4.4",
"@angular/platform-browser-dynamic": "^4.4.4",
"@angular/router": "^4.4.4",
"@ngx-translate/core": "^8.0.0",
"@ngx-translate/http-loader": "^2.0.0",
"core-js": "^2.5.1",
"font-awesome": "^4.7.0",
"primeng": "^4.2.1",
"quill": "^1.3.2",
"rxjs": "^5.4.3",
"zone.js": "^0.8.18"
错误
ERROR in Error: Internal error: unknown identifier undefined
at Object.importExpr$$1 [as importExpr] (...\node_modules\@angular\compiler\bundles\compiler.umd.js:24211:23)
at tokenExpr (...\node_modules\@angular\compiler\bundles\compiler.umd.js:18577:39)
at providerDef (...\node_modules\@angular\compiler\bundles\compiler.umd.js:18480:20)
at ...\node_modules\@angular\compiler\bundles\compiler.umd.js:18697:77
at Array.map (<anonymous>)
at NgModuleCompiler.compile (...\node_modules\@angular\compiler\bundles\compiler.umd.js:18697:44)
at AotCompiler._compileModule (...\node_modules\@angular\compiler\bundles\compiler.umd.js:24144:32)
at ...\node_modules\@angular\compiler\bundles\compiler.umd.js:24056:66
at Array.forEach (<anonymous>)
at AotCompiler._compileImplFile (...\node_modules\@angular\compiler\bundles\compiler.umd.js:24056:19)
at ...\node_modules\@angular\compiler\bundles\compiler.umd.js:23969:87
at Array.map (<anonymous>)
at AotCompiler.emitAllImpls (...\node_modules\@angular\compiler\bundles\compiler.umd.js:23969:52)
at CodeGenerator.emit (...\node_modules\@angular\compiler-cli\src\codegen.js:42:46)
at ...\node_modules\@angular\compiler-cli\src\codegen.js:33:61
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
这部分应用程序导致了这个问题:
export function windowFactory(): any {
return window;
}
providers: [{
provide: Window,
useFactory: windowFactory
}],
我在Angular 6项目中遇到了同样的错误。
当您运行“ng build --prod”时,请仔细阅读警告消息。它在哪个文件中抛出警告消息(例如:dataservice.ts)。
然后转到那个(dataService.ts)文件并删除@Injectable。
@Injectable({
providedIn: 'root'
})
然后尝试再次构建。这个对我有用。
只是为了完整性,因为我在不同的上下文中偶然发现了相同的错误。使用已导出为default
的已升级angularjs服务类时,我的构建中发生错误
// Service definition
export default class MyService { /* Registered AngularJS service here */ }
// Module
import MyService from './my-service'
// Upgrade
export function myServiceFactory(i:any) {
return i.get('myService');
}
// ngUpgraded Angular main module
@NgModule({
providers: [
{ provide: MyService, useFactory: myServiceFactory, deps: ['$injector'] }
]
})
删除默认导出解决了该问题。
尝试提供注射日期而不是注射令牌时出现相同的错误。
所以我做了:
providers: [
{ provide: Date, useValue: new Date() }
]
并改为
providers: [
{ provide: NOW, useValue: new Date() },
],
其中NOW
在服务中定义,依赖于它
export const NOW = new InjectionToken<Date>('now');
所有记录在Angular site。
检查模板可能您在模板中使用了未在组件中定义的变量
对我来说,我为使用HttpClient的服务创建了一个基类。
基类和子类被标记为Injectable
。
我从基类中删除了Injectable
,它解决了这个问题。
在im4ever的回答中,我遇到了同样的问题。它仅在设置配置时进行构建,而不是在ng serve
或ng build
上,这让我相信它是一个AOT问题。
当我使用工厂方法注册服务时,我正在这样做以便我可以注入localStorage,我的模块设置就像这样(为简洁起见缩短了)。
providers: [
{provide: StateService, useFactory: provideStateService}
],
我解决这个问题的方法是删除我在@Injectable({})
类定义中使用的StateService
装饰器。一旦我删除它,错误都消失了。
在为Angular 7.1.4运行ng build --prod时遇到此问题,所以我删除了Karthi Coimbatore为我创建的TooltipService推荐使用Angular Material工具提示位置的@Injectable。