1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

App.settings - the Angular way?

Discussão em 'Angular' iniciado por Royi Namir, Setembro 15, 2024.

  1. Royi Namir

    Royi Namir Guest

    I want to add an App Settings section into my App where It will contain some consts and pre defined values.

    I've already read this answer which uses OpaqueToken But it is deprecated in Angular. This article explains the differences but it didn't provide a full example , and my attempts were unsuccessful.

    Here is what I've tried ( I don't know if it's the right way) :

    //ServiceAppSettings.ts

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

    const CONFIG = {
    apiUrl: 'http://my.api.com',
    theme: 'suicid-squad',
    title: 'My awesome app'
    };
    const FEATURE_ENABLED = true;
    const API_URL = new InjectionToken<string>('apiUrl');


    And this is the component where I want to use those consts :

    //MainPage.ts

    import {...} from '@angular/core'
    import {ServiceTest} from "./ServiceTest"

    @Component({
    selector: 'my-app',
    template: `
    <span>Hi</span>
    ` , providers: [
    {
    provide: ServiceTest,
    useFactory: ( apiUrl) => {
    // create data service
    },
    deps: [

    new Inject(API_URL)
    ]
    }
    ]
    })
    export class MainPage {


    }


    But it doesn't work and I get errors.

    Question:

    How can I consume "app.settings" values the Angular way?

    plunker

    NB Sure I can create Injectable service and put it in the provider of the NgModule , But as I said I want to do it with InjectionToken , the Angular way.

    Continue reading...

Compartilhe esta Página