-
Notifications
You must be signed in to change notification settings - Fork 9k
22.10 import()的浏览器实现代码可能有误 #638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
不需要用setTimeout。只要把 textContent 换成 PS. 其实不建议书里放 |
|
第一种方式,测试可以;第二种方式,**另外一种方式是不用 onload,而直接在代码里回调一个全局钩子而不是写一个全局变量。**方便给下代码? |
@xianshenglu 就跟 jsonp 的实现原理类似。 |
这段代码其实是提案给出的。https://github.com/tc39/proposal-dynamic-import#using-host-specific-mechanisms 我测了一下, 贺老师提出的全局钩子是可行的替代方法。 我把这一段删了。 |
@ruanyf 我去开了个issue:tc39/proposal-dynamic-import#61 |
阮老师,

你好,我在第22章,Module 的语法,第10小节中,发现
import()的浏览器实现
代码可能有问题,截图如下:代码:
我觉得有两点可能需要修改:
最后一行代码的用法我几乎没见过,觉得可能有问题,即
document.documentElement.appendChild(script);
为什么不用document.body.appendChild(script);
?这里是重点:
上述代码中,
script.onload
和script.onerror
永远不会被触发,因为script
没有指定src
,采用的是行内脚本,最终导致了返回的promise
永远不会resolve
,这就导致上面的代码是失效的,老师可以本地测试下;当然如果采用src
就需要另外的文件了,似乎也不太方便;如果把
script.onload
和script.onerror
去掉,直接resolve(window[tempGlobal])
也是失败的,因为window[tempGlobal]
是undefined
,所以猜测这行代码import * as m from "${url}";
是异步执行的,只有等待import
动作结束返回结果并把值赋给window[tempGlobal]
时,才能resolve(window[tempGlobal])
,但是我并没有找到方法可以检测到import
动作完成,所以只能设个定时器检测window[tempGlobal]
,所以最终,我把代码改成下面这样:这样的情况下,我本地测试时成功的,测试的代码:
importModule("./test2.js").then(exportObj => console.log(exportObj));
嗯,我说完了。
The text was updated successfully, but these errors were encountered: