我有这些数据:
r4
我要做的是在ngOnInit上创建一个循环,这样我就可以动态地进行多次导入。
从数据
中获取的myData.path等看起来就像这样myData = [
{
"name": "namehere"
"path": "somepath",
"const": "someconst",
"method": "somemethod"
""
},
{
"name": "othernamehere"
"path": "othersomepath",
"const": "othersomeconst",
"method": "othersomemethod"
""
}
];
答案 0 :(得分:2)
您可以尝试forEach
功能:
myData.forEach((data,index) => {
import(data.path).then(module => {
this.myData[index].const = new module.data.name().data.method();
});
})
答案 1 :(得分:2)
我们可以按需异步导入和加载模块
def get_queryset(self):
longitude = self.request.query_params.get('longitude')
latitude= self.request.query_params.get('latitude')
radius = self.request.query_params.get('radius')
location = Point(longitude, latitude)
queryset = Model.objects.filter(location__distance_lte=(location, D(m=distance))).distance(location).order_by('distance')
return queryset
答案 2 :(得分:1)
建议您尝试使用@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();
});
这里我用于...用它的回调替换foreach,并等待替换缩进的承诺级别。
在函数前面需要异步:
async ngOnInit() {
管理await的使用。