A solution that worked for me
Create a folder named app-config
under libs and add an 'index.ts` file inside app-config folder. This lib can be shared across all your apps. Add the following content inside the index.ts file
import { InjectionToken } from '@angular/core';
export const APP_CONFIG = new InjectionToken('Application config');
Open base tsconfig.json
file and add the path for app-config so that it can be useful for importing into your app with @app-workspace/app-config
"paths": {
"@app-workspace/ui": ["libs/ui/src/index.ts"],
"@app-workspace/auth": ["libs/auth/src/index.ts"],
"@app-workspace/utils": ["libs/utils/src/index.ts"],
"@app-workspace/app-config": ["libs/app-config/index.ts"]
}
Now inside your apps open the file under apps/app1/src/app/app.module.ts
and make the following changes for the providers array
import { APP_CONFIG } from '@app-workspace/app-config';
import { environment } from '../environments/environment';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
],
providers: [
{ provide: APP_CONFIG, useValue: environment}
],
bootstrap: [AppComponent]
})
export class AppModule {}
Here is the sample environment.ts
file that is residing under app1
export const environment = {
production: false,
apiUrl: 'http://localhost:3000/api',
};
You can use app-config inside your shared libs as-well, for example lets say you are making api calls from within your shared lib libs/auth/src/lib/services/auth.service.ts
import { APP_CONFIG } from '@app-workspace/app-config';
import { Inject, Injectable } from '@angular/core';
@Injectable()
export class AuthService {
constructor(
@Inject(APP_CONFIG) private appConfig: any
) {
console.log(this.appConfig.apiUrl); // This will print `http://localhost:3000/api`
}
}
Hope this is helpful :) Also on a sidenote, you may have to restart your application if you get any error while using the imports.