如何在运行时同步导入TypescriptJavascript?

问题描述 投票:0回答:1

我有一个类在Typescript中,我试图使一个同步导入,但导入是异步的。我正在尝试这个。

--------------100 lines of code--------------------

import('../../../x/y/z').then((x) => { 
     alert('Component')
});

--------------100 lines of code----------------------

但是,使用这种方法的警报被打印在最后,即后200行得到执行(异步,因为内部的Promise被使用),但我想执行它在给定的串行顺序。

谢谢,谢谢

javascript typescript dynamic-import
1个回答
0
投票

你只需要提前导入,从导入的模块中调用同步函数就可以了。

// hello.js
export default function hello() { return "Hello, world!" }

// index.js
import hello from "./hello"
hello()
© www.soinside.com 2019 - 2024. All rights reserved.