首页 > 解决方案 > 如何从打字稿中的文件导入对象的对象

问题描述

如何从打字稿中的文件导入对象的对象。我知道我可以像import house from 'a/b/c/house'. 但我可以只导入parent吗?所以我不需要像这样写代码 house.grandparent.parent.xxx

#filepath: a/b/c/house.ts
const house = {
  grandparent: {
    parent: {
      childa: (text: string) => `s'${text}')`,
      childb: 'b',
      childc: 'c',
    },
  },
};

export default house;

标签: javascripttypescripttypescript2.0typescript1.8

解决方案


您可以拆分对象。

// house.ts
export const parent = {
  childa: (text: string) => `s'${text}')`,
  childb: "b",
  childc: "c"
};

export const house = {
  grandparent: {
    parent: parent
  }
};

// some-consumer.ts
import { house, parent } from "./house";

console.log(house);
console.log(parent);

推荐阅读