处理 systemjs 加载时间和顺序 - 必须有更好的方法

dealing with systemjs load time and sequence - there must be a better way

我有一个使用 system.jsangular 应用程序的情况,我需要能够在每页的基础上包含特定的 System.import(f) 语句,但这是必要的它们都包含在 before angular 完成引导并加载其模块。

我在这个问题上花了很长时间,最终这是我设计的解决方法;

在我的 config.js 中,我正在这样做...

config.js

// .... configuration ... //

Promise.all([
    System.import('jquery'),
    System.import('angular'),
    System.import('angular.ng'),
    System.import('bootstrap')
]).then(function() {
    $(document).on('angular', function() {
        angular.bootstrap(document, ['module-name']); $('body').removeClass('cloak');
    });
});

然后我有一个 css class 名为 .cloak。我尝试了 ng-cloak 但发现它没有完成工作(我怀疑是因为我推迟了 angular 引导)

.cloak { visibility: hidden; }

然后在我的个人页面上,我使用这种代码来加强我的页面特定导入并完成该过程。

<!-- Load SystemJS -->
<script src="assets/js/system.js"></script>
<script src="config.js"></script>
<script>
    System.import('app/main').then(function (e) {
        Promise.all([
            System.import('app/controllers/index'),
            System.import('app/controllers/read'),
            System.import('app/controllers/edit/article'),
            System.import('scripts/lib/init')
        ]).then(function (m) {
            $(document).trigger('angular');
        });
    });
</script>

这就是基本的想法;在通过 system.js 导入所有内容之前,我不允许 angular 完成其模块的接线。目前,这个似乎工作正常。

我的问题是我不是一个很好的程序员,也不是很聪明。这似乎是 system.js 设计方式带来的一个非常标准、正常的问题,我突然想到 是一个更好的内置解决方案,我' m 丢失或未找到。

有没有其他人处理过这个可以提供一些建议?

我需要推迟模块接线的原因是因为我有各种 angular 控制器和模型等等,我不希望它们 所有 在每一页上加载。 (我的意思是,除了依赖解析之外,将脚本缩减为仅需要的脚本是模块加载器的要点之一)。

尝试使用 ng-app 指令总是会引起麻烦 - 因为 angular 会在所有内容完成并加载之前尝试加载,所以它很难找到某些控制器等东西在某些页面上。

我需要替代方法的最大原因是这种方法不能很好地缩小打字稿。

一般来说,这是 ng-cloak 的一个非常标准的问题。也许更好的方法已经出现,但自从我开始使用 angular 以来,我刚刚在我的 index.html 文件的 header 中定义了 ng-cloak,如下所示:

<style>
[ng\:cloak],[ng-cloak],[data-ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak {
    display: none !important;
}
</style>

并将 class="ng-cloak" 添加到我页面的 body 标签中。它工作得很好,当 Angular 完成加载时,它的内部 ng-cloak 样式有效地覆盖了我的样式。

似乎将内联脚本基本上按原样移动到外部文件,仍然会允许它 运行 与您拥有的完全一样,然后进行缩小,这样就可以了。

听起来您并不是在此处创建 SPA,因为如果我理解正确的话,您是从每个页面调用 config.jss。您可能需要重新考虑并选择 SPA。您可能最终会预先加载所有内容,但您只会加载一次。