Load Config JSON File In Angular 2

后端 未结 5 1646
闹比i
闹比i 2020-12-05 05:50

I want to load Constant File in Angular 2(which is a Normal TypeScript File) having WebAPI EndPoints. In Angular1.x. we used to have constants for the same. How in Angular 2

5条回答
  •  感动是毒
    2020-12-05 06:23

    I had same issue and in the end i give up from .ts and put it in .js :D like this:

    configuration.js in root

    var configuration = {
        'apiHost': 'http://localhost:8900',
        'enableInMemoryWebApi': false,
        'authMode': 'standalone',
        'wsUrl': 'ws://localhost:8900/ws'
    };
    
    module.exports = configuration;
    

    in .ts file for ex. user.service.ts

    let configuration = require('../configuration'); //in import section
    @Injectable()
    export class UserService {
        ...
        getUser(id: number | string): Promise {
            console.log(configuration.apiHost) //will get propertye from .js file
            return this.http.get(`${configuration.apiHost}/${id}`, this.headers).toPromise().then(this.extractData).catch(this.handleError);
        }
    }
    

    Hope it helps

提交回复
热议问题