gpt4 book ai didi

dart - Dart 中的代码分割

转载 作者:行者123 更新时间:2023-12-02 01:45:28 25 4
gpt4 key购买 nike

有什么方法可以在 Dart 中进行某种代码分割吗?我想推迟加载一些很少使用的代码以加速初始代码下载。在 Javascript 中,我会注入(inject)一个新的 <script>标签,在 GWT 中我只需调用 GWT.runAsync() 。 Dart 中有类似的东西吗?

根据this link , <script>注入(inject)不起作用(“每个 HTML 页面最多可以有一个 Dart 脚本标签”,“我们不支持动态注入(inject)加载 Dart 代码的标签。”)。我还找到了this fixed issue声称:“第一个[用例]是延迟加载,以避免在稍后需要或仅在某些情况下需要某些代码时进行大量下载。我们现在有一种机制。”。不幸的是,我找不到任何关于如何实现这一点的信息。有谁知道这方面的情况吗?

最佳答案

2014 年 9 月更新: this has been fixed!

Dart 现在可以使用特殊的 import...deferred 语法轻松支持延迟加载。例如:

import analytics.dart deferred as analytics
void main(){
analytics.loadLibrary.then((_) { // future
// code ready
enableAnalyticsControl()
});
}

这里是an official tutorial关于使用延迟加载。

<小时/>

恐怕您想要做的事情仍然是不可能的(假设您没有使用 dart2js)。

参见this issue .

As Kasper said in comment 3, so far this has been discussed a deployment feature which you will get with dart2dart. The VMs involvement in supporting this ends with giving the dart2dart generated code access to loading sources lazily through a library call. This library API still needs to be specified though.

如果您正在使用 dart2js,则可以完成此操作。 Here is a blog post on how to do this.

const lazy = const DeferredLibrary('reverser', uri: './part.js');

然后你可以调用lazy.load().then((_) { ...

关于dart - Dart 中的代码分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com