簡體   English   中英

使用SystemJS從TypeScript組件導入文件夾

[英]Import folder from TypeScript components using SystemJS

我使用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配置問題,但找不到設置。

嘗試將index.d.ts重命名為index.ts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM