Bogdan B
Bogdan B

Reputation: 934

How to use relative path to files inside Angular Library?

I've created a library for theming, in which i'm changing the stylesheet path depending on the selected theme. My problem is that the path inside my library is assets/themes, but if I add the library to a project, that is not working anymore since it points to the project's assets/themes instead of the library's. Is there a way to relatively reference that path inside the library?

Upvotes: 2

Views: 1259

Answers (1)

Adrita Sharma
Adrita Sharma

Reputation: 22213

You can keep the relative path in environment.ts and then pass it to the library module using forRoot.

Suppose you want to use the path in libService or any other file as per your requirement,

Try like this:

app.module.ts

import { NgModule } from '@angular/core';
import { LibModule } from 'some-library';

@NgModule({
    imports: [
        libModule.forRoot({
            environment: environment
        })
    ]
})

LibModule.module.ts

export class LibModule{ 
  static forRoot(config): ModuleWithProviders {
    return {
      ngModule: LibModule,
      providers: [LibService, {provide: CONFIG, useValue: config}]
    };
  }
}

libService.service :

export const CONFIG = new InjectionToken<any>('config');

export class LibService{

  constructor(@Inject(CONFIG) private config:any) {
    console.log(config)
   }
}

Upvotes: 2

Related Questions