在我们的Angular 2应用中,我们遇到了构建错误,但只有在使用" ng build --prod"时,才会使用" ng build"或者" ng serve"。错误消息是:
ERROR in Error encountered resolving symbol values statically. Expression form not supported (position 27:55 in the original .ts file), resolving symbol RestService in [...]/src/app/shared-modules/service/rest/rest.service.ts
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\LPROG\Ldev\Projekte\svn\hippo_branch\frontend\shared\src'
@ ./src/main.ts 5:0-74
@ multi ./src/main.ts
相关的代码部分是:
constructor(
private http: Http,
private mappingService: MappingService,
private backendUrlService: BackendUrlService,
@Inject('RestServiceConfig') public config: {rest_api_name: string}) {
我可以将其更改为:
constructor(
private http: Http,
private mappingService: MappingService,
private backendUrlService: BackendUrlService,
@Inject('RestServiceConfig') public config: any) {
这适用于" ng build --prod"而且我认为我们可以忍受这种变化(虽然我还有其他的错误,那些不应该相关),但我想知道为什么会这样,并希望有强大的打字。有人可以向我解释一下吗?
ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.0.0-rc.1
node: 6.9.4
os: win32 x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/cli: 1.0.0-rc.1
@angular/compiler-cli: 2.4.10
答案 0 :(得分:0)
使用ng build --prod
时出现错误的原因是因为该命令也意味着--aot
开关。
Angular的Ahead of Time编译要求您的代码可以静态分析。回报是包装尺寸的显着减少。我建议您按如下方式更改代码,这样可以为您提供强大的输入和工作AOT。还有一个可重复使用的类型:
// Before your class
export interface Config {
rest_api_name: string;
}
// In your class
constructor(
private http: Http,
private mappingService: MappingService,
private backendUrlService: BackendUrlService,
@Inject('RestServiceConfig') public config: Config) {