angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Error encountered resolving symbol values statically

Open shouvikme opened this issue 9 years ago • 22 comments

Please provide us with the following information:

OS?

Mac OSX (El Capitan)

Versions.

@angular/cli: 1.0.0-beta.30 node: 7.0.0 os: darwin x64 @angular/common: 2.4.6 @angular/compiler: 2.4.6 @angular/core: 2.4.6 @angular/forms: 2.4.6 @angular/http: 2.4.6 @angular/platform-browser: 2.4.6 @angular/platform-browser-dynamic: 2.4.6 @angular/router: 3.4.6 @angular/cli: 1.0.0-beta.30 @angular/compiler-cli: 2.4.6

Repro steps.

Added a simple Opaque Token and NG-BUILD failed as the PROVIDER

import { OpaqueToken } from '@angular/core';

export const SUMMARRY = new OpaqueToken('summary');
const dictionary = { };

// providers
export const SUMMARY_PROVIDER = 
[
    { provide: SUMMARY, useValue: dictionary },
];

The log given by the failure.

ERROR in Error encountered resolving symbol values statically. Reference to a local (non-exported) symbol 'dictionary'. Consider exporting the symbol (position 14:7 in the original .ts file), resolving symbol SUMMARY_PROVIDERS

Any other information

To workaround this:

  1. I put the dictionary inside a single quotes
  2. Ran ng-build and then ng-serve
  3. The code failed as it is now a string
  4. Went back to code and removed the single quotes
  5. Webpack did not complain and the code works fine

shouvikme avatar Feb 05 '17 18:02 shouvikme

The solution my team found was to translate our const into a class.

We had an app consuming a package (built by us, installed by the consumer app via a private npm package). The package exported an object like this:

export const constants = {
    SOME_CONST: "foo",
    SOME_OTHER_CONST: "bar"
}

Our app imported that like import { constants } from '@ourcompany/common-package' Running ng serve produced the error ERROR in Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler, resolving symbol constants

The solution was to instead export the following:

export class Constants {
    public static SOME_CONST = "foo";
    public static SOME_OTHER_CONST = "bar";
}

For some reason, the compiler can't statically analyze an exported const, but has no problem with an exported class.

Hope this helps!

dmayerdesign avatar Oct 10 '17 19:10 dmayerdesign

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.