<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>angular &#8211; Rafael Bernard Araujo</title>
	<atom:link href="https://rafael.bernard-araujo.com/tag/angular/feed" rel="self" type="application/rss+xml" />
	<link>https://rafael.bernard-araujo.com</link>
	<description>desenvolvendo... while(!success){  try(); }</description>
	<lastBuildDate>Wed, 31 Jul 2024 09:14:14 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">21941730</site>	<item>
		<title>Environment Variables in Angular</title>
		<link>https://rafael.bernard-araujo.com/environment-variables-in-angular.php</link>
					<comments>https://rafael.bernard-araujo.com/environment-variables-in-angular.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Fri, 24 Jan 2020 01:20:27 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=1342</guid>

					<description><![CDATA[Need to use different values depending on the environment you’re in? If you’re building an app that needs to use API host URLs depending on the environment, you may do it easily in Angular using the environmen.ts file. We are considering Angular 8+ apps for this article. Angular CLI projects already use a production environment [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Need to use different values depending on the environment you’re in? If you’re building an app that needs to use API host URLs depending on the environment, you may do it easily in Angular using the <code>environmen.ts</code> file.</p>
<p>We are considering Angular 8+ apps for this article.</p>
<p>Angular CLI projects already use a production environment variable to enable production mode when in the production environment at <code>main.ts</code>:</p>
<pre><code class="language-ts">if (environment.production) {
  enableProdMode();
}</code></pre>
<p>And you'll also notice that by default in the <code>src/environment</code> folder you have an environment file for development and one for production. Let's use this feature to allow us to use different API host URL depending if we're in development or production mode:</p>
<p><code>environment.ts</code>:</p>
<pre><code class="language-ts">export const environment = {
  production: false,
  apiHost: https://api.local.com
}</code></pre>
<p><code>environment.prod.ts</code>:</p>
<pre><code class="language-ts">export const environment = {
  production: true,
  apiHost: https://api.production-url.com
};</code></pre>
<p>And in our <code>app.component.ts</code> all we have to do in order to access the variable is the following:</p>
<pre><code class="language-ts">import { Component } from &#039;@angular/core&#039;;
import { environment } from &#039;../environments/environment&#039;;

@Component({ ... })
export class AppComponent {
  apiHost: string = environment.apiHost;
}</code></pre>
<p>Now in development mode the apiHost variable resolves to <code>https://api.local.com</code> and in production resolves to <code>https://api.production-url.com</code>. You may run <code>ng build --prod</code> and check.</p>
<p><strong>Detecting Development Mode</strong></p>
<p>Angular also provides us with an utility function called <code>isDevMode</code> that makes it easy to check if the app in running in dev mode:</p>
<pre><code class="language-ts">import { Component, OnInit, isDevMode } from &#039;@angular/core&#039;;

@Component({ ... })
export class AppComponent implements OnInit {
  ngOnInit() {
    if (isDevMode()) {
      console.log(&#039;Development!&#039;);
    } else {
      console.log(&#039;Cool. Production!&#039;);
    }
  }
}</code></pre>
<p><strong>Adding a Staging Environment</strong></p>
<p>To add a new environment in Angular projects a new entry to <code>configuration</code> property should be added at <code>angular.json</code> file. Let's add a staging environment for example. Note that <code>production</code> property already exists.</p>
<pre><code class="language-json">&quot;configurations&quot;: {
  &quot;production&quot;: {
    &quot;optimization&quot;: true,
    &quot;outputHashing&quot;: &quot;all&quot;,
    &quot;sourceMap&quot;: false,
    &quot;extractCss&quot;: true,
    &quot;namedChunks&quot;: false,
    &quot;aot&quot;: true,
    &quot;extractLicenses&quot;: true,
    &quot;vendorChunk&quot;: false,
    &quot;buildOptimizer&quot;: true,
    &quot;fileReplacements&quot;: [
      {
        &quot;replace&quot;: &quot;src/environments/environment.ts&quot;,
        &quot;with&quot;: &quot;src/environments/environment.prod.ts&quot;
      }
    ]
  },
  &quot;staging&quot;: {
    &quot;optimization&quot;: true,
    &quot;outputHashing&quot;: &quot;all&quot;,
    &quot;sourceMap&quot;: false,
    &quot;extractCss&quot;: true,
    &quot;namedChunks&quot;: false,
    &quot;aot&quot;: true,
    &quot;extractLicenses&quot;: true,
    &quot;vendorChunk&quot;: false,
    &quot;buildOptimizer&quot;: true,
    &quot;fileReplacements&quot;: [
      {
        &quot;replace&quot;: &quot;src/environments/environment.ts&quot;,
        &quot;with&quot;: &quot;src/environments/environment.stating.ts&quot;
      }
    ]
  }</code></pre>
<p>And now we can add a staging environment file and suddenly be and build the project with <code>ng build --configuration=staging</code> on our CI (or deploy process) to deploy on staging environment:</p>
<p><code>environment.staging.ts</code></p>
<pre><code class="language-ts">export const environment = {
  production: false,
  apiHost: https://staging.host.com
};</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/environment-variables-in-angular.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1342</post-id>	</item>
	</channel>
</rss>
