GWT:延迟加载外部JS资源 [英] GWT: deferred loading of external JS resources

查看:82
本文介绍了GWT:延迟加载外部JS资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小部件取决于一些外部JS文件,我想懒加载所有这些外部资源。我已经使用代码拆分来延迟加载与窗口小部件有关的GWT代码,但是使用脚本标记在gwt.xml中定义的JS文件仍然会被加载,这是不可取的。



是否有标准的GWT方式来按需加载这些外部资源?我可以使用原生JS自己做,但我宁愿不花时间。

我们希望看看 com.google.gwt.core.client.ScriptInjector 类。 来自javadoc


动态创建一个脚本标记并将其附加到DOM。



...



使用脚本作为URL加载:



 ScriptInjector.fromUrl(http://example.com/foo.js).setCallback(
new callback< Void,Exception>(){
public void onFailure(Exception reason){
Window.alert(Script load failed。);
}
public void onSuccess(Void result){
Window.alert(脚本加载成功。);
}
})。inject();

这段代码当然可以从你的分割点或者代码中的任何地方调用。 / p>

I have a widget depending on some external JS files, and I'd like to lazy load all these external resources. I've already used code splitting to lazy load the GWT code that concerns the widget, but the JS files defined in the gwt.xml, using the script tag, are loaded anyway, which is not desirable.

Is there a standard GWT way of loading these external resources on demand? I can do it myself using raw JS, but I'd rather not spend time on this too.

解决方案

I think you'll want to take a look at the com.google.gwt.core.client.ScriptInjector class. From the javadocs:

Dynamically create a script tag and attach it to the DOM.

...

Usage with script loaded as URL:

   ScriptInjector.fromUrl("http://example.com/foo.js").setCallback(
     new Callback<Void, Exception>() {
        public void onFailure(Exception reason) {
          Window.alert("Script load failed.");
        }
        public void onSuccess(Void result) {
          Window.alert("Script load success.");
        }
     }).inject();

This code can of course be invoked from within your split points, or indeed anywhere in your code.

这篇关于GWT:延迟加载外部JS资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆