是否可以在Dart中延迟使用JS库? [英] is it possible to lazily use JS libs with Dart?
问题描述
我正在使用chartjs(具有dart界面 https://pub.dartlang.org/packages/ chartjs ),并尝试通过将< script src = chartjs.js< / script>
注入头部来使其延迟,
我遇到了这个异常:无法读取未定义的属性'Chart'。
I am using chartjs (with the dart interface https://pub.dartlang.org/packages/chartjs) and trying to make it deferred by injecting a <script src="chartjs.js"></script>
into the head section and awaiting it's load event to then use the lib.
I am getting this exception: Cannot read property 'Chart' of undefined.
那么,是否有可能在Dart加载后加载JS库?
So, is it possible to load a JS lib after Dart loaded?
推荐答案
找到了更好的方法!
让我们删除定义
变量装入dart后,然后在添加async时,任何第三方库都可以工作:D
lets remove the define
variable after dart loads, then any third-party lib works when added async :D
将此变量添加到main()中:
add this to your main():
import 'dart:js';
void main() {
context.callMethod('fixRequireJs');
}
并在index.html中:
and in your index.html:
<script type="text/javascript">
window.fixRequireJs = function()
{
console.log('define is ', typeof define);
if (typeof define == 'function') {
console.log('removing define...');
delete define;
window.define = null;
}
}
</script>
这篇关于是否可以在Dart中延迟使用JS库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!