How to use environment variable in index.html for

2019-04-19 02:22发布

I am using angular6, in my project I am using Facebook Account Toolkit for mobile verification purpose.

I need to initialise Account toolkit in index.html file using following code.

  AccountKit.init({
   appId:"XX",
   state:"xx",
   version:"v1.2",
   fbAppEventsEnabled:true,
   debug:true
 });

The problem is, values for appId and state change depending on environment (development/test/production).

How can I use environment variables in index.html file.

Please let me know if anyone has a solution for angular 6.

Thanks in advance.

3条回答
Lonely孤独者°
2楼-- · 2019-04-19 02:54

import your environment file into .ts file.

import { environment } from '../../environments/environment';

Create required fields in your class, assign values from environment to these variables in the constructor, use usual binding in the .html file.

.ts

import { Component } from '@angular/core';
import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public production = true;
  constructor() {
    this.production = environment.production;
  }
}

.html

<span>{{production}}</span>
查看更多
孤傲高冷的网名
3楼-- · 2019-04-19 03:12

I think you can do it all in main.ts

const env = environment;

 AccountKit.init({
   appId:env.appId,  // this lane
   state:env.state,  // this lane
   version:"v1.2",
   fbAppEventsEnabled:true,
   debug:true
});

Thanks.

查看更多
【Aperson】
4楼-- · 2019-04-19 03:19

You should create copy of index.html and name it index.someenv.html, Than in your angular.json in environment configuration setup file replacement:

"fileReplacements": [
            {
              "replace": "src/index.html",
              "with": "src/index.someenv.html"
            }
          ]

And angular cli will replace this files when you'll run build

查看更多
登录 后发表回答