【问题标题】:Import folder from TypeScript components using SystemJS使用 SystemJS 从 TypeScript 组件导入文件夹
【发布时间】:2016-10-16 12:13:43
【问题描述】:

我使用 Angular2、TypeScript 和 SystemJS 作为模块加载器。出于调试原因,我主要在这里使用 SystemJs 来保持文件“原样”。它基本上可以工作,但我在加载组件时遇到了特定问题。我在这里阅读了几个答案,但没有发现任何可比性。

我的应用程序有这个结构:

请注意组件文件夹中的“index.d.ts”。原因只是 app.ts 文件中的简化导入,如下所示:

import * as cmp from './Components';

该文件夹包含我的所有组件和如下所示的“index.d.ts”:

export * from './shop-root';
export * from './shop-catalogs';
export * from './shop-edit-cat';
export * from './shop-about';
export * from './shop-contact';

我是这样使用我的组件的,然后(摘自@NgModule):

declarations: [
    cmp.ShopRootComponent,
    cmp.ShopCatalogs,
    cmp.ShopEditCat,
    cmp.ShopAbout,
    cmp.ShopContact
]

从打字稿的角度来看,这工作正常,这里没有问题。我的打字稿编译器很高兴并创建了一个包含导入的“app.js”文件:

var cmp = require('./Components');

当 SystemJS 开始加载时,它无法加载文件夹。

如果我使用单个文件,一切都会完美无缺。所以所有基本路径等都是正确的。

但是,最终有数百个组件,我认为单个文件加载会使我的 TypeScript 代码变得混乱。

使用 WebPack 或其他加载器不是答案。我面临的问题是编写漂亮的 TypeScript 文件的改进,并让已编译的 JavaScript 便于调试(和学习)。

当前的 System.config.js 如下所示:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'assets/js/lib/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'assets/js/app',
            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            // other libraries
            'rxjs': 'npm:rxjs',
            'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './app.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            },
            'angular-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

同样,对于被称为单个文件的组件,此配置运行良好。

问题

如何使用 SystemJS 从文件夹/目录 加载组件?我怀疑这主要是 SystemJs 配置问题,但找不到设置。

【问题讨论】:

    标签: angular typescript systemjs


    【解决方案1】:

    尝试将index.d.ts 重命名为index.ts

    【讨论】:

    • 非常感谢。不知道 TS 编译器的这种行为。作为补充: import * as cmp from './Components';必须改为 import * as cmp from './Components/index';。虽然 TS 不在乎,但它让 SystemJS 很高兴。
    猜你喜欢
    • 2016-12-11
    • 2020-06-24
    • 2016-11-04
    • 2021-04-19
    • 2021-09-30
    • 2018-12-11
    • 2017-09-09
    • 2019-12-11
    • 2016-05-28
    相关资源
    最近更新 更多