קטגוריה: angulr/cli

Config file for Angular CLI with AOT

Angular CLI supports const environment but you need to
compile the project for each environment.  But the IT guys dont want to run again and again the CLI for each environment.

The recipe:


const config = {
    url :


2.  Add the file to index.html, like this.

<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
    <script src="assets/config.js"></script>
3.  Now you can use the config variable everywhere. 

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
declare let config:{url:string};
if (environment.production) {
console.log(`config url : ${config.url}`);
    .catch(err => console.log(err));

Impotent: Don’t import the config.js like this: import {config} from './assets/config.js'; Because it will insert the file to the bundles files that CLI creates.
What you think?
מודעות פרסומת
2 תגובות

פורסם ע"י ב- אוקטובר 22, 2017 ב- Angular 2.0, angulr/cli