11

我正在尝试通过https://github.com/ng-bootstrap/ng-bootstrap将引导模块添加到我的 ng2 应用程序中。但一直收到此错误:

在此处输入图像描述

这是我的索引文件,也许我的文件有错误?索引.html:

<html>
<head>
    <title>MyApplication</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">-->
    <link href="font-awesome/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">
    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/@ng-bootstrap/ng-bootstrap"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function(err){ console.error(err); });
    </script>
</head>
<!-- 3. Display the application -->
<body>
    <app>Loading...</app>
</body>
</html>

完整的 systemjs.config

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function(global) {
    // map tells the System loader where to look for things
    var map = {
        'app':                        'app', // 'dist',
        '@angular':                   'node_modules/@angular',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        'rxjs':                       'node_modules/rxjs',
        '@ng-bootstrap':              'node_modules/ng2-bootstrap'
    };
    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                        { main: 'main.js',  defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
    };
    var ngPackageNames = [
        'common',
        'compiler',
        'core',
        'forms',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'router-deprecated',
        'upgrade',
    ];
    // Individual files (~300 requests):
    function packIndex(pkgName) {
        packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    // Bundled (~40 requests):
    function packUmd(pkgName) {
        packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }
    // Most environments should use UMD; some (Karma) need the individual index files
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    // Add package entries for angular packages
    ngPackageNames.forEach(setPackageConfig);
    var config = {
        map: map,
        packages: packages
    };
    System.config(config);
})(this);

4

2 回答 2

12

我遇到过同样的问题。基本上你需要告诉你的 systemjs.config 在哪里可以找到 ng-bootstrap 的所有单个组件。

根据 ulubeyn 的回答,我在基本的 systemjs.config 中添加了以下内容:

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  // map tells the System loader where to look for things
  var map = {
    'app': 'app', // 'dist',
    '@angular': 'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs': 'node_modules/rxjs',
    '@ng-bootstrap': 'node_modules/@ng-bootstrap',
    '@ng-bootstrap/ng-bootstrap': 'node_modules/@ng-bootstrap/ng-bootstrap'
  };
  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app': {main: 'main.js', defaultExtension: 'js'},
    'rxjs': {defaultExtension: 'js'},
    'angular2-in-memory-web-api': {main: 'index.js', defaultExtension: 'js'},
    '@ng-bootstrap/ng-bootstrap': {main: 'index.js', defaultExtension: 'js'}

  };
  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'forms',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'router-deprecated',
    'upgrade',
  ];

  var ngBootstrapPackageNames = [
    'accordion',
    'alert',
    'bundles',
    'buttons',
    'carousel',
    'collapse',
    'dropdown',
    'esm',
    'modal',
    'pagination',
    'popover',
    'progressbar',
    'rating',
    'tabset',
    'timepicker',
    'tooltip',
    'typeahead',
    'util'
  ];
  // Individual files (~300 requests):
  function packIndex(pkgName) {
    packages['@angular/' + pkgName] = {main: 'index.js', defaultExtension: 'js'};
  }

  // Bundled (~40 requests):
  function packUmd(pkgName) {
    packages['@angular/' + pkgName] = {main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js'};
  }

  function ngBootstrapPackIndex(pkgName) {
    packages['@ng-bootstrap/ng-bootstrap/' + pkgName] = {main: 'index.js', defaultExtension: 'js'};
  }

  // Most environments should use UMD; some (Karma) need the individual index files
  var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
  // Add package entries for angular packages
  ngPackageNames.forEach(setPackageConfig);
  ngBootstrapPackageNames.forEach(ngBootstrapPackIndex);

  var config = {
    map: map,
    packages: packages
  };
  System.config(config);
})(this);

详细地:

  1. 添加'@ng-bootstrap/ng-bootstrap': 'node_modules/@ng-bootstrap/ng-bootstrap'到您的地图。这将为您的 ng-bootstrap 提供路径。

  2. 添加'@ng-bootstrap/ng-bootstrap': { main: 'index.js', defaultExtension: 'js' }到您的包裹中。

  3. 添加一个提供所有 ng-bootstrap 组件文件夹的新数组(请参阅上面示例中的 ngBootstrapPackageNames)。

  4. 现在通过将这些信息添加到您的地图并将其与相应的索引文件相关联来将所有内容组合在一起:

    function ngBootstrapPackIndex(pkgName) {
       packages['@ng-bootstrap/ng-bootstrap/' + pkgName] = {main: 'index.js', defaultExtension: 'js'};
    }
    ngBootstrapPackageNames.forEach(ngBootstrapPackIndex);
    

我希望这会有所帮助,因为它适用于我的这些变化。

ng-bootstrap alpha 5 的更新

如果您使用的是 alpha 5,请将变量中的@ng-bootstrap/ng-bootstrap映射更改packages为此;

var packages = {
     ...,
     '@ng-bootstrap/ng-bootstrap': {main: '/bundles/ng-bootstrap.js', defaultExtension: 'js'},
     ...
}
于 2016-08-15T17:03:21.433 回答
4

您的 systemjs 文件应如下所示:

var ngBootstrapMap = {
    '@ng-bootstrap/ng-bootstrap': '../lib/@ng-bootstrap/ng-bootstrap' //change the path according to your project structure
}

var ngBootstrapPackages = {
    '@ng-bootstrap/ng-bootstrap': { main: 'index.js', defaultExtension: 'js' }
};

var ngBootstrapPackageNames = [
    'accordion',
    'alert',
    'bundles',
    'buttons',
    'carousel',
    'collapse',
    'dropdown',
    'esm',
    'modal',
    'pagination',
    'popover',
    'progressbar',
    'rating',
    'tabset',
    'timepicker',
    'tooltip',
    'typeahead',
    'util'
];

function ngBootstrapPackIndex(pkgName) {
    ngBootstrapPackages['@ng-bootstrap/ng-bootstrap/' + pkgName] = { main: 'index.js', defaultExtension: 'js' };
}

ngBootstrapPackageNames.forEach(ngBootstrapPackIndex);

var ngBootstrapConfig = {
    map: ngBootstrapMap,
    packages: ngBootstrapPackages
};

System.config(ngBootstrapConfig);

基本上,它会在 @ng-bootstrap/ng-bootstrap 文件夹中查找 index.js 文件,然后对于每个组件,它会在 @ng-bootstrap/ng-bootstrap/{componentName} 中查找 index.js 文件

希望对你有帮助

于 2016-08-12T14:14:10.283 回答