18

从我的库执行“ng build”时出现以下错误。

我目前正在使用 Angular 6,但我相信这个错误与版本无关。

/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1:为导出符号“KeycloakService”生成的元数据中遇到错误:
/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12:收集的元数据包含将报告的错误运行时:只能引用已初始化的变量和常量,因为模板编译器需要此变量的值。
{"__symbolic":"error","message":"变量未初始化","line":17,"character":11} 错误:/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/ pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1:为导出的符号“KeycloakService”生成的元数据中遇到错误:
/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects /pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12: Metadata collection contains a error that will return at runtime: Only initialized variables and constants can be referenced because this variable的值是模板编译器需要。
{"__symbolic":"error","message":"变量未初始化","line":17,"character":11} 在 /home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@ angular/compiler-cli/src/metadata/collector.js:707:31 at Array.forEach () at validateMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/ src/metadata/collector.js:695:46) 在 MetadataCollector.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/metadata/collector.js:550 :21) 在 MetadataCache.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_cache.js:86:41) 在 Object.在 Object.readMetadata (/home/rafaelvicio/desenv/ my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_reader.js:46:37) 在 TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules /@angular/compiler-cli/src/transformers/compiler_host.js:464:38) 在 StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot /static_symbol_resolver.js:480:49) 在 StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:268:33)33)33)在 TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (/home/rafaelvicio/desenv/ my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:464:38) 在 StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules /@angular/compiler/src/aot/static_symbol_resolver.js:480:49) 在 StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver .js:268:33)在 TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (/home/rafaelvicio/desenv/ my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:464:38) 在 StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules /@angular/compiler/src/aot/static_symbol_resolver.js:480:49) 在 StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver .js:268:33)在 StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/ my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:480:49) 在 StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@角度/编译器/src/aot/static_symbol_resolver.js:268:33)在 StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/ my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:480:49) 在 StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@角度/编译器/src/aot/static_symbol_resolver.js:268:33)_createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:268:33)_createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:268:33)

keycloak.service.ts:

import { Injectable } from '@angular/core';
import { KeycloakLoginOptions } from './keycloak.d';

import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

import * as Keycloak from './keycloak';

export type KeycloakClient = Keycloak.KeycloakInstance;
type InitOptions = Keycloak.KeycloakInitOptions;

@Injectable()
export class KeycloakService {

    constructor(private http: HttpClient) { }

    static keycloakAuth: KeycloakClient;

.... More code here
4

5 回答 5

34

// @dynamic在声明静态方法的类之前添加

于 2018-10-29T10:53:34.137 回答
4

我正在使用 Angular V8 并且遇到了同样的问题。Angular 不允许不初始化静态变量。解决方案是使用 null 进行初始化以避免错误。

@Injectable()
export class ExampleService {

    static URL: string = null;
...
于 2020-02-04T16:08:30.483 回答
4

util我今天在一个提供静态方法作为助手的类中遇到了这个问题。

我通过替换thisObjectUtils(类名本身)解决了它,它可以工作。

同样正如@NicolasThierion 在@stack247 评论的回复中提到的那样,您还可以尝试以下技巧

// @dynamic
export class SomeClass {
    public static get() { return 'someValue'; } 
}
于 2019-06-05T04:06:51.030 回答
0

我的经验是在方法的构造函数中错误地使用了静态值。this.STATIC_VALUE通过而不是引用静态值Foo.STATIC_VALUE

class Foo {
  static STATIC_VALUE = 'hi';

  constructor(
    private bar: string
  ) {}

  static defoo(): Foo {
    return Foo(this.STATIC_VALUE);
  }
}

方法 defoo 应该是

  static defoo(): Foo {
    return Foo(Foo.STATIC_VALUE);
  }
于 2021-08-17T12:56:26.723 回答
-1

我遇到了同样的问题,并通过从变量中删除“静态”来解决问题。

于 2018-09-19T20:19:17.353 回答