问题描述
有什么办法到某种类型的代码分裂在Dart吗?我想推迟加载一些很少使用的代码加速初始代码下载。在Javascript中,我会注入一个新的< script>
标签,在GWT我只是调用 GWT.runAsync()
。 Dart中有类似的东西吗?
Is there any way to to some kind of code splitting in Dart? I'd like to defer the loading of some rarely used code to accelerate the initial code download. In Javascript, I'd inject a new <script>
tag, in GWT i'd just call GWT.runAsync()
. Is there something similar in Dart?
根据,< script>
注入无效(每个HTML页面最多只能有一个Dart脚本标记,我们不支持动态注入一个加载Dart代码的标签。我还发现声明:最初的一个[用例]被延迟加载,以避免在稍后需要某些代码时进行大量下载,或者在某些情况下只需要这样做,我们现在有了一个机制。不幸的是,我找不到任何有关如何实现这一点。
According to this link, <script>
injection won't work ("Each HTML page can have at most one Dart script tag", "We do not support dynamically injecting a tag that loads Dart code."). I also found this fixed issue claiming: "The initial one [use case] is deferred loading, to avoid massive downloads when some code is needed only later, or perhaps only needed in some situations. We now have a mechanism for this.". Unfortunately, I couldn't find anything on how to implement this. Does anyone know anything about this?
推荐答案
2014年9月更新:
Dart现在可以使用特殊的 import ... deferred
语法轻松支持延迟加载。例如:
Dart now easily supports deferred loading with special import... deferred
syntax. For example:
import analytics.dart deferred as analytics
void main(){
analytics.loadLibrary.then((_) { // future
// code ready
enableAnalyticsControl()
});
}
这里是。
恐怕你想要做的事情仍然是不可能的(假设你不使用dart2js)。
I'm afraid what you're trying to do is still not possible (assuming you're not using dart2js that is) .
a href =https://code.google.com/p/dart/issues/detail?id=10171 =nofollow noreferrer>此问题。
See this issue.
如果你使用dart2js,可以这样做。 这是有关如何执行此操作的博客文章。 / a>
If you are using dart2js this can be done. Here is a blog post on how to do this.
const lazy = const DeferredLibrary('reverser', uri: './part.js');
这篇关于Dart中的代码拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!