Using InjectionToken results in the generation of the error message "Encountered an issue while resolving symbol values

Recently, I encountered an issue while building my Ionic 3 app using ionic cordova build ios --prod.

Everything was functioning perfectly until a package update caused some complications, preventing me from successfully building the app.

I suspect that the error is stemming from an InjectionToken within my app.config.ts file, which I implemented based on the guidelines outlined in Angular 4 documentation.

Here's a snippet from app.config.ts:

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

export interface OneSignalConfig {
  apiKey: string
};

// PROD Config

export const ONESIGNAL_CONFIG: OneSignalConfig = {
  apiKey: '**apiKey**'
};

export const firebaseConfig = {
  ...
};

export const mixpanelToken = "**mytoken**";

// COMMON
export let ONESIGNAL_CONFIG_TOKEN = new InjectionToken<OneSignalConfig>('onesignal.config');

In my app.module.ts file, I utilized these configurations:

// Config imports
import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config.ts';
...
// the providers part
providers: [
    ...
    OneSignal,
    { provide: ONESIGNAL_CONFIG_TOKEN, useValue: ONESIGNAL_CONFIG },
...
  ]

However, attempting to build the app resulted in the following issue:

[16:10:19]  ngc started ...
[16:10:27]  typescript error
            Error encountered resolving symbol values statically. Could not resolve ./app-config.ts relative to
            /Users/me/myproj/src/app/app.module.ts., resolving symbol
            AppModule in /Users/me/myproj/src/app/app.module.ts,
            resolving symbol AppModule in
            /Users/me/myproj/src/app/app.module.ts, resolving symbol
            AppModule in /Users/me/myproj/src/app/app.module.ts

Error: The Angular AoT build failed. See the issues above
    at /Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:237:55
    at step (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:32:23)
    at Object.next (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:13:53)
    at fulfilled (/Users/me/myproj/node_modules/@ionic/app-scripts/dist/aot/aot-compiler.js:4:58)
[16:10:27]  copy finished in 7.84 s

Strangely enough, the app functions as expected when using serve, run, and emulate. This inconsistency is quite perplexing to me.

Answer №1

Here is a simplified version:

import { InjectionToken } from "@angular/core";

    export let APP_CONFIG= new InjectionToken<AppConfig >('app.config');

    export interface AppConfig {
      apiKey: string
      // Add any other constants here.
    }

    export const AppConstants: AppConfig = {
      apiKey: '**apiKey**'
      // Add any other values matching the interface above.
    }

In your module:

// Import config settings
import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config.ts';
...
// Set up providers
providers: [
    ...
    OneSignal,
    { provide: APP_CONFIG, useValue: AppConstants },
...
  ]

In your components:

...

import { Inject } from '@angular/core';
import { APP_CONFIG, AppConfig } from '../../app/app.config';

...

constructor(@Inject(APP_CONFIG)
    private config: AppConfig,
) {
  console.log(this.config.apiKey) // Use constants as needed.
}

Answer №2

It is important to note that file extensions should not be included in your import statement. To resolve the issue, simply remove `.ts` from app.module.ts. This adjustment should allow your code to function properly.

import { firebaseConfig, ONESIGNAL_CONFIG, ONESIGNAL_CONFIG_TOKEN } from './app-config';

If you encounter a different error message containing `ts`, it suggests that you may be working with an older version of Angular.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to verify within the "if" statement without repeating the code?

Is there a way in javascript (or typescript) to prevent redundant object rewriting within an if statement condition? For example: if (A != null || A != B) { // do something here } // can it be done like this: if (A != null || != B) { // avoid repeating ...

Is it possible to enlarge the panel only by clicking on the text, without affecting the entire header panel?

I need help with my accordion setup. I want to be able to expand or collapse each panel by clicking only on the header text, not the entire header area. Can anyone provide guidance on how to achieve this? For example, when I click on the text for 'He ...

Begin the Angular 4 project by loading it with RequireJS

I am currently in the process of constructing an Angular4 application using the ng command. Successfully, I can build and execute it with ng serve. Now, my aim is to incorporate RequireJS so that the Angular4 application can load and run smoothly. Despit ...

What is the reason behind `ngAfterViewChecked` and `ngAfterContentChecked` being invoked twice?

import { AfterContentChecked, AfterViewChecked, Component } from '@angular/core'; @Component({ selector: 'app-root', template: '' }) export class AppComponent implements AfterViewChecked, AfterContentChecked { ngA ...

Adding typing to Firebase Functions handlers V2: A step-by-step guide

Here's a function I am currently working with: export async function onDeleteVideo(event: FirestoreEvent<QueryDocumentSnapshot, { uid: string }>): Promise<any> { if (!event) { return } const { disposables } = event.data.data() ...

Sometimes the downloaded xlsx file may become corrupted

Currently, I am working on developing a project using Angular4 with Typescript. One of the tasks involved creating a blob utilizing the XLSX-populate library. Below is an example showing the code snippet for generating a valid xlsx object: var url = wind ...

Tips for obtaining response headers

Currently, I am utilizing Angular version 15.0 and retrieving a list of items from the backend (ASP.NET Core 5) with an additional item attached to the header. The GET method in the client-side service is as follows: /** GET Paged commodities from the s ...

Encountering "SyntaxError:" due to an unexpected token in the JSON data at the beginning while trying to parse with JSON.parse

Encountering a "SyntaxError:" Unexpected token in JSON at position 0 when fetching compressed data from backend Spring Boot in Angular 7 On the backend, I am compressing a list of objects using the following structure: ArticleObj.java public class Articl ...

Can someone please point me in the right direction to locate a Typescript project within Visual Studio

I've been struggling with this issue for days and still can't find a solution. After installing the Typescript tool for Visual Studio 2015, it appears to be successfully installed. https://i.stack.imgur.com/nlcyC.jpg However, I am unable to loc ...

How can I apply concatMap in Angular?

Can you please guide me on how to effectively utilize concatMap with getPrices() and getDetails()? export class HistoricalPricesComponent implements OnInit, OnDestroy { private unsubscribe$ = new Subject < void > (); infoTitle ...

Determine the date and time based on the number of days passed

Hey there! I have a dataset structured like this: let events = { "KOTH Airship": ["EVERY 19:00"], "KOTH Castle": ["EVERY 20:00"], Totem: ["EVERY 17:00", "EVERY 23:00"], Jum ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

Creating an interface or type in Typescript with a nested object property that uses keys from the same interface

I am looking to create an interface or type that can restrict the nested object properties based on keys defined in the main interface. class MyClass implements MyInterface { prop1: string; promp2: number; nestedObj: { prop1: string; // Allowed a ...

Having trouble activating the ENTER key press event listener for the QuillJS editor in Angular 4 and above

I have been trying to set up an event listener for the ENTER key press in QuillJS using addBinding as described in the official documentation here. However, despite successfully configuring listeners for other keys like BACKSPACE, I am unable to get the EN ...

The alignment of elements side by side in Angular Flex is not supported, specifically in versions Angular 8.2.1 and Flex-Layout 8.0.0.beta 26

I'm attempting to arrange my boxes in a single line, with the ability to wrap and switch to column mode on smaller phone screens. Desired layout: Wins Losses Ties Points On shrinking screen: Wins Losses Ties Points Code ...

Developing React component libraries with TypeScript compared to Babel compiler

Currently, I'm utilizing the babel compiler for compiling my React component libraries. The initial choice was influenced by Create React App's use of the same compiler. However, I've encountered challenges with using babel for creating libr ...

Guide to customizing the appearance of a Bootstrap Component within an Angular project

Is it possible to completely customize the style/CSS of a component from ng-bootstrap? Here's the issue I'm facing: I have this code... <ngb-datepicker #dp [(ngModel)]="model" (navigate)="date = $event.next" style ...

Retrieve the initial token from a union, referred to as an "or list," in Typescript

Is there a way to define a generic type F with the following behavior: type X = F<'a'|'b'|'c'> should result in X being 'a'. And if type X = F<'alpha'|'beta'|'gamma'|'del ...

Mocked observables are returned when testing an Angular service that includes parameters

I'm currently exploring various types of unit testing and find myself struggling with a test for a service once again. Here is the function in my service that I need to test: Just to clarify: this.setParams returns an object like {name: 'Test&ap ...

Getting started with Angular 2 using NPM version 3.10.6 and Angular CLI 1.0.0

I am having trouble when I run 'NPM start,' all I get is https://i.sstatic.net/QCViF.png Below are the files in my project: package.json { "name": "angular2-quickstart", "version": "1.0.0", // rest of the package.json file continues... } ...