网站搜索

从另一个 Typescript 文件访问数组


我在我的一个 Typescript 文件中有一个数组 DPS,它在该文件中没有定义在任何类中,只是独自存在。

const DPS: DataTable[] = [
  { tDataPoint: "d1", tICCP: "i1", tStartDate: "s1", tEndDate: "e1" },
  { tDataPoint: "d2", tICCP: "i2", tStartDate: "s2", tEndDate: "e2" },
  { tDataPoint: "d3", tICCP: "i3", tStartDate: "s3", tEndDate: "e3" },
  { tDataPoint: "d4", tICCP: "i4", tStartDate: "s4", tEndDate: "e4" }
];

在另一个不同组件目录下的 Typescript 文件中,我希望访问这个数组以对其进行操作。

答案#1

我能够在声明变量数组时在 const 前添加 export。然后将变量导入到另一个 Typescript 文件中 import { DPS } from '../mainpage/mainpage.component';。然后我想对该数组做的只是删除一个条目,所以为了操作这个数组,我只是使用点表示法。DPS.splice(0,1);。

答案#2

你可以使用 export const DPS: DataTable[]。