简体   繁体   English

如何挂钩到SystemJs.import的完成事件?

[英]how to hook into complete event of SystemJs.import?

The onready function of my webpage looks like this: 我的网页的就绪功能如下所示:

$(function(){
    importJS();
    init();
});

The importJS() function on my page looks like this: 我页面上的importJS()函数如下所示:

function importJS()
{
  // set js ref path
  SystemJS.config({
    baseURL: '/js/'
  });

  //load js modules
  SystemJS.import('Init.js');   
}

The Init.js lib has an init() function but when my onready function runs, the init() function call does not find the init() function. Init.js库具有init()函数,但是当我的onready函数运行时,init()函数调用未找到init()函数。 I stepped through Chrome Dev Tools and it looks like importJS() hasn't finished loading Init.js by the time init() gets called. 我逐步浏览了Chrome Dev Tools,看来在调用init()时importJS()尚未完成Init.js的加载。 What's the trick to making this code work? 使此代码有效的诀窍是什么? Ie how can I ensure that SystemJS has fully loaded Init.js by the time init() gets called? 即我如何确保在调用init()时SystemJS已完全加载Init.js?

SystemJS.import returns a promise, the easiest way to call init() after import has finished is to return that promise from importJS() and call init() in its then callback: SystemJS.import返回一个promise,在导入完成后调用init()的最简单方法是从importJS()返回该promise并在其then回调中调用init()

$(function(){
    importJS().then(function() {
        init();
    }
});

function importJS()
{
  // set js ref path
  SystemJS.config({
    baseURL: '/js/'
  });

  //load js modules
  return SystemJS.import('Init.js');   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM