Error encountered in metadata generated for exported symbol when constructing an angular 6 library
Asked Answered
D

5

18

I'm getting the following error while performing 'ng build' from my library.

I'm using Angular 6 at the moment, but I believe this error is not version related.

/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1: Error encountered in metadata generated for exported symbol 'KeycloakService':
/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12: Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.
{"__symbolic":"error","message":"Variable not initialized","line":17,"character":11} Error: /home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1: Error encountered in metadata generated for exported symbol 'KeycloakService':
/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12: Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler.
{"__symbolic":"error","message":"Variable not initialized","line":17,"character":11} at /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) at MetadataCollector.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/metadata/collector.js:550:21) at MetadataCache.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_cache.js:86:41) at Object.getSourceFileMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:112:56) at Object.readMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_reader.js:46:37) at TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:464:38) at StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:480:49) at StaticSymbolResolver._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
Daubigny answered 7/5, 2018 at 21:54 Comment(0)
O
34

add // @dynamic before the class that declares static methods

Osmanli answered 29/10, 2018 at 10:53 Comment(2)
would be good to hear why this resolves the problemSchuh
For me, it had to do with static methods. Adding // @dynamic solves it.Allot
C
5

I encountered this issue today in an util class providing static methods as helpers.

I solved it by replacing this with ObjectUtils (the class name itself), and it works.

Also as @NicolasThierion mentioned in his reply commented by @stack247, you could also try the following hack

// @dynamic
export class SomeClass {
    public static get() { return 'someValue'; } 
}
Carducci answered 5/6, 2019 at 4:6 Comment(0)
M
4

I'm using Angular V8 and had the same problem. Angular does not allow a static variable to not be initialized. The solution was to initialize with null to avoid errors.

@Injectable()
export class ExampleService {

    static URL: string = null;
...
Methylnaphthalene answered 4/2, 2020 at 16:8 Comment(2)
This doesn't work when the "variable" it points to is a getter function!Tericaterina
Thanks, it's work. Just add default value, maybe null.Amphora
S
0

My experience with this was using a static value incorrectly in the constructor in a method. Referring to the static value by this.STATIC_VALUE rather than Foo.STATIC_VALUE.

class Foo {
  static STATIC_VALUE = 'hi';

  constructor(
    private bar: string
  ) {}

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

The method defoo should have been

  static defoo(): Foo {
    return Foo(Foo.STATIC_VALUE);
  }
Scleritis answered 17/8, 2021 at 12:56 Comment(0)
F
-1

I was having the same issue and resolved the problem by removing 'static' from the variable.

Factorial answered 19/9, 2018 at 20:19 Comment(1)
Removing static fundamentally changes the nature of how/where you use the variable.Darfur

© 2022 - 2024 — McMap. All rights reserved.