Angular 如何将字符串数组外部化为角度

Angular 如何将字符串数组外部化为角度,angular,ngx-translate,Angular,Ngx Translate,我是英语初学者。关于字符串外部化的文章很多,但没有一篇回答了我的疑问。我也查过了。我的上级告诉我将代码中的所有字符串外部化。我已经外部化了直接或硬编码字符串,但也有两个字符串数组。我将向您展示我的代码(*最小代码): timeselector.component.html <h2 [translate]="'timeselection.tshead'"></h2> 这很容易。但是在我的typescript文件中有一些数组。我被告知也要将它们外部化。代码如下: times

我是英语初学者。关于字符串外部化的文章很多,但没有一篇回答了我的疑问。我也查过了。我的上级告诉我将代码中的所有字符串外部化。我已经外部化了直接或硬编码字符串,但也有两个字符串数组。我将向您展示我的代码(*最小代码):

timeselector.component.html

<h2 [translate]="'timeselection.tshead'"></h2>
这很容易。但是在我的typescript文件中有一些数组。我被告知也要将它们外部化。代码如下:

timeselector.component.ts

import { Component } from '@angular/core';

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

  ...

  public arr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  public source = ['Calendar year', 'Year-to-date', 
  'Fiscal Year-To-Date', 'Fiscal Calendar Year', 'Rolling 12 months'];

  ...
}
请看,我这里有两个数组
arr
source
。如果可行的话,请帮助我将这两种方法中的任何一种具体化


PS:我们正在使用这两个包
“@ngx translate/core”:“11.0.1”
“@ngx translate/http loader”:“4.0.0”

您需要加载en.json文件,然后将其存储到某个位置(阵列/本地存储)。接下来,您需要从该阵列/存储器中搜索所需的密钥。具体问题是什么?字符串数组包含字符串。因此,将每个字符串外部化。你是在问如何使用ngx翻译出一个模板吗?你读过吗?请注意,
[translate]=“'timeselection.tshead'”
可以简化为可读性更高的
translate=“timeselection.tshead”
我相信OP希望将其数组外部化为.ts文件:-)这在我的脑海中提出了一组全新的问题。我希望将字符串外部化为我在问题中展示的同一个json文件。
import { Component } from '@angular/core';

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

  ...

  public arr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  public source = ['Calendar year', 'Year-to-date', 
  'Fiscal Year-To-Date', 'Fiscal Calendar Year', 'Rolling 12 months'];

  ...
}