Z
Z
zhdoon2019-05-22 16:04:35
Angular
zhdoon, 2019-05-22 16:04:35

Is there a way to create a config file that can be used across all components and services in angular 7?

What would I like.
Create a class MainConfig with a set of properties and then use it in all components/services of the application.
Something like this.
File /src/app/main.config.ts:

export class MainConfig {

    prodUrl: string;

    constructor() { 
        this.prodUrl = 'https://site.api';
    }     
    
}

Use in my.component.ts like this
import { MainConfig } from '../main.config';

export class ApiService {

    constructor(private mainConfig: MainConfig) { }

    res = this.myMethod(this.mainConfig.prodUrl){....}

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shvets, 2019-05-22
@zhdoon

Create a service, store data in it and inject it everywhere. What is the problem.
Everything is like in your example, only

@Injectable({
  providedIn: 'root'
})
export class MainConfig {
  // данные
}

S
sdgroup14, 2019-05-22
@sdgroup14

If you created an application using cli then you have 2 files for developer and production in the src folder in the environments folder. In the object, specify all those global configs that you need. Sockets, api, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question