Angular Access data from Service

前端 未结 3 504
猫巷女王i
猫巷女王i 2021-01-26 11:52

I\'m using angular 5 and httpclient. I\'m not a big fan of the built in environments to set up my environment api url\'s that I need to consume. I have switched to using an ngin

3条回答
  •  情歌与酒
    2021-01-26 12:50

    A simple way to do it is like this:

    class Constants {
        public api1: string;
        public api2: string;
    
        public setApi1(api: string) {}
        public setApi2(api: string) {}
    }
    
    export const CONSTANTS: Constants = {
        api1: "DEFAULT API 1",
        api2: "DEFAULT API 2",
        setApi1(api: string) : void {
            this.api1 = api;
        },
        setApi2(api: string) : void {
            this.api2 = api;
        }
    }
    

    Then just import your const wherever you need the persistence:

    import {CONSTANTS} from "../path/to/constant.ts";
    

提交回复
热议问题