Angular 如何在单独的文件中标记常量

Angular 如何在单独的文件中标记常量,angular,Angular,我需要将以下所有数据声明为一个单独的常量文件,并将其用作ts/controller文件之一 this.flags = {}; this.flags['NL'] = "flag-icon flag-icon-nl"; this.flags['BE'] = "flag-icon flag-icon-be"; this.flags['DE'] = "flag-icon flag-icon-de"; this.flags['FR'] = "flag-icon flag-i

我需要将以下所有数据声明为一个单独的常量文件,并将其用作ts/controller文件之一

this.flags = {};
    this.flags['NL'] = "flag-icon flag-icon-nl";
    this.flags['BE'] = "flag-icon flag-icon-be";
    this.flags['DE'] = "flag-icon flag-icon-de";
    this.flags['FR'] = "flag-icon flag-icon-fr";
    this.flags['SE'] = "flag-icon flag-icon-se";
    this.flags['ES'] = "flag-icon flag-icon-es";


if(this.routingConfiguratioService.countryCode==='BE'){
      this.labels["ILB"] = "Athlon Car Lease";
      this.labels["DAL"] = "Belfius Auto Lease NV";
      this.labels["C4P"] = "Cars4Publicity BVBA";
    } else if(this.routingConfiguratioService.countryCode==='LU'){
       this.labels["LUX"] = "Athlon Car Lease Lux";
    } else{
       this.labels["ACL"] = "Athlon Car Lease";
       this.labels["WPL"] = "Wagenplan";
    }

要声明常量值,需要创建单独的
constant.ts
文件

常数。ts

export const flags {
     static  abc:string = 'ABC'
 }
// You can also simplify your object like this to avoid repetitions by using String Interpolation

const flagIcon = "flag-icon flag-icon-";

export const flags = {
  "NL": `${flagIcon}nl`,
  "BE": `${flagIcon}be`,
  "DE": `${flagIcon}de`,
  "FR": `${flagIcon}fr`,
  "SE": `${flagIcon}se`,
  "ES": `${flagIcon}es`
};
现在在组件中导入上述文件并访问如下值

flags.abc;

希望这会有帮助

要声明常量值,需要创建单独的
constant.ts
文件

常数。ts

export const flags {
     static  abc:string = 'ABC'
 }
// You can also simplify your object like this to avoid repetitions by using String Interpolation

const flagIcon = "flag-icon flag-icon-";

export const flags = {
  "NL": `${flagIcon}nl`,
  "BE": `${flagIcon}be`,
  "DE": `${flagIcon}de`,
  "FR": `${flagIcon}fr`,
  "SE": `${flagIcon}se`,
  "ES": `${flagIcon}es`
};
现在在组件中导入上述文件并访问如下值

flags.abc;

希望这会有帮助

只需创建如下类型的
const

export const flags = {
  'NL': "flag-icon flag-icon-nl",
  'BE': "flag-icon flag-icon-be",
  'DE': "flag-icon flag-icon-de",
  'FR': "flag-icon flag-icon-fr",
  'SE': "flag-icon flag-icon-se",
  'ES': "flag-icon flag-icon-es",
};

export const labels = {
  "ILB": "Athlon Car Lease",
  "DAL": "Belfius Auto Lease NV",
  "C4P": "Cars4Publicity BVBA",
  "LUX": "Athlon Car Lease Lux",
  "ACL": "Athlon Car Lease",
  "WPL": "Wagenplan",
}
然后将它们导入组件中:

import { Component } from '@angular/core';
import { flags, labels } from './flags.const';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  ngOnInit() {
    console.log('flags: ', flags);
    console.log('labels: ', labels);
    /*if(this.routingConfiguratioService.countryCode==='BE'){
      this.labels["ILB"] = "Athlon Car Lease";
      this.labels["DAL"] = "Belfius Auto Lease NV";
      this.labels["C4P"] = "Cars4Publicity BVBA";
    } else if(this.routingConfiguratioService.countryCode==='LU'){
       this.labels["LUX"] = "Athlon Car Lease Lux";
    } else{
       this.labels["ACL"] = "Athlon Car Lease";
       this.labels["WPL"] = "Wagenplan";
    }*/
  }

}

这是给你的裁判的一封信


只需创建如下类型的
const

export const flags = {
  'NL': "flag-icon flag-icon-nl",
  'BE': "flag-icon flag-icon-be",
  'DE': "flag-icon flag-icon-de",
  'FR': "flag-icon flag-icon-fr",
  'SE': "flag-icon flag-icon-se",
  'ES': "flag-icon flag-icon-es",
};

export const labels = {
  "ILB": "Athlon Car Lease",
  "DAL": "Belfius Auto Lease NV",
  "C4P": "Cars4Publicity BVBA",
  "LUX": "Athlon Car Lease Lux",
  "ACL": "Athlon Car Lease",
  "WPL": "Wagenplan",
}
然后将它们导入组件中:

import { Component } from '@angular/core';
import { flags, labels } from './flags.const';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';

  ngOnInit() {
    console.log('flags: ', flags);
    console.log('labels: ', labels);
    /*if(this.routingConfiguratioService.countryCode==='BE'){
      this.labels["ILB"] = "Athlon Car Lease";
      this.labels["DAL"] = "Belfius Auto Lease NV";
      this.labels["C4P"] = "Cars4Publicity BVBA";
    } else if(this.routingConfiguratioService.countryCode==='LU'){
       this.labels["LUX"] = "Athlon Car Lease Lux";
    } else{
       this.labels["ACL"] = "Athlon Car Lease";
       this.labels["WPL"] = "Wagenplan";
    }*/
  }

}

这是给你的裁判的一封信


您可以使用
export

数据.常数.ts

export const flags {
     static  abc:string = 'ABC'
 }
// You can also simplify your object like this to avoid repetitions by using String Interpolation

const flagIcon = "flag-icon flag-icon-";

export const flags = {
  "NL": `${flagIcon}nl`,
  "BE": `${flagIcon}be`,
  "DE": `${flagIcon}de`,
  "FR": `${flagIcon}fr`,
  "SE": `${flagIcon}se`,
  "ES": `${flagIcon}es`
};
组件

import { flags } from './data.constant'


@Component({...})
export class ChildComponent {

    flagList = flags;    // assign it here or you can console it on your constructor

    constructor() {
       console.log(flags);
       console.log(this.flagList);
    }

}

您可以使用
export

数据.常数.ts

export const flags {
     static  abc:string = 'ABC'
 }
// You can also simplify your object like this to avoid repetitions by using String Interpolation

const flagIcon = "flag-icon flag-icon-";

export const flags = {
  "NL": `${flagIcon}nl`,
  "BE": `${flagIcon}be`,
  "DE": `${flagIcon}de`,
  "FR": `${flagIcon}fr`,
  "SE": `${flagIcon}se`,
  "ES": `${flagIcon}es`
};
组件

import { flags } from './data.constant'


@Component({...})
export class ChildComponent {

    flagList = flags;    // assign it here or you can console it on your constructor

    constructor() {
       console.log(flags);
       console.log(this.flagList);
    }

}

请勾选下面能解决问题的答案可能会有帮助请勾选下面能解决问题的答案可能会有帮助无需将常量对象包装在类中谢谢。Toledo没有必要将常量对象包装在类中。托莱多