Javascript 角度5-数据在ngOnInit中循环

Javascript 角度5-数据在ngOnInit中循环,javascript,angular,typescript,angular5,Javascript,Angular,Typescript,Angular5,我有以下数据: myData = [ { "name": "namehere" "path": "somepath", "const": "someconst", "method": "somemethod" "" }, { "name": "othernamehere" "path": "othersomepath", "const": "othersomeconst", "method": "othersome

我有以下数据:

myData = [
 {
    "name": "namehere"
    "path": "somepath",
    "const": "someconst",
    "method": "somemethod"
    ""
  },
  {
    "name": "othernamehere"
    "path": "othersomepath",
    "const": "othersomeconst",
    "method": "othersomemethod"
    ""
  }
];
我要做的是在ngOnInit上创建一个循环,以便动态地执行多个导入

从数据中获取myData.path等,看起来像这样

ngOnInit() {

    import(myData.path).then(module => {
      const myData.const = new module.myData.name().myData.method();
    });

  }

您可以尝试
forEach
功能:

myData.forEach((data,index) => {
    import(data.path).then(module => {
        this.myData[index].const = new module.data.name().data.method();
    });
})
async ngOnInit() {

建议您尝试与byt@vivek建议的解决方案相同的解决方案,但采用最新的ES版本方法:

for(const [index, data] of Object.entries(myData)) {
  const module = await import(data.path);
  myData[index].const = new module.data.name().data.method();
});
这里我使用for…of用它的回调替换foreach,并等待替换缩进的承诺级别

函数前面需要异步:

myData.forEach((data,index) => {
    import(data.path).then(module => {
        this.myData[index].const = new module.data.name().data.method();
    });
})
async ngOnInit() {

管理wait在it中的使用。

我们可以根据需要异步导入和加载模块

myData = [
 {
    "name": "namehere"
    "path": "somepath",
    "const": "someconst",
    "method": "somemethod"
    ""
  },
  {
    "name": "othernamehere"
    "path": "othersomepath",
    "const": "othersomeconst",
    "method": "othersomemethod"
    ""
  }
];

myData.map((module)=>{
   import(module.path+'/'+module.name).then(module => {
      // Here you should use an array and assign each module to an array
      // which you can use later to use it's exported methods
    });
})

我收到这样的警告:“关键依赖项:依赖项的请求是一个表达式”,而您在@vivek的解决方案中没有得到它?