简体   繁体   English

如何从 typescript 文件中导入 object 的 object

[英]How to import object of object from file in typescript

How can I import object of object from file in typescript.如何从 typescript 的文件中导入 object 的 object。 I know I can import like import house from 'a/b/c/house' .我知道我可以像import house from 'a/b/c/house'一样导入。 but can I just import parent ?但我可以只导入parent吗? so I do not need to write the code like house.grandparent.parent.xxx所以我不需要写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;

You could split the objects.您可以拆分对象。

// 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);

No, Unfortunately import statements does not work like object destructuring.不,不幸的是导入语句不像 object 解构那样工作。

but you can do this:但你可以这样做:

const constParent = require('a/b/c/house').grandparent.parent;

Hope I can help you希望我能帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM