6

我已按照 angular material2 Getting Started 中的说明安装@angular/material。通过 Atom,我更新了 package.json、app.module,除了入门说明之外,我还更新了 systemjs.config,如下:'@angular/material':'node_modules/@angular/material',

我收到这些错误: zone.js:1274 GET http://localhost:3000/node_modules/@angular/material/ 404 (Not Found)

(SystemJS) XHR 错误 (404 Not Found) 加载http://localhost:3000/node_modules/@angular/material (...)

我相信我已经跟踪到许多@angular 文件夹都有一个包含 umd 文件的 bundle 子文件夹的问题,但 @angular/material 文件夹没有 bundle 子文件夹。因此,“解包”功能找不到@angular/material/material.umd.js

systemjs 超出了我的舒适区,所以我不确定上述内容,但我无法使用 alpha.9-3 的新文件结构解决 404 问题这是相关代码(其他组件未显示)。

包.json

 "@angular/material": "2.0.0-alpha.9-3",

应用程序模块

 import { MaterialModule } from '@angular/material';
 @NgModule({
  imports: MaterialModule.forRoot(),

systemjs.config

  (function(global) {
    var map = {
      'app': 'app',
      // angular bundles
      '@angular':                   'node_modules/@angular',
      // other libraries
      'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
      'rxjs':                       'node_modules/rxjs',
      '@angular/material': 'node_modules/@angular/material',
    };
    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',
     'platform-server',
     'router',
     'upgrade',
    ]; //adding 'material' to the array causes a different 404 error
    function packIndex(pkgName) {
      packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
    }
    function packUmd(pkgName) {
      packages['@angular/'+pkgName] = { main: 'bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
    }
    var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
    ngPackageNames.forEach(setPackageConfig); 
    var config = {
      map: map,
      packages: packages
    };
    System.config(config);

  })(this); //end of function(global)

我检查了我的文件夹/文件结构,@angular/material/material.umd.js 肯定在那里。如何摆脱 404 not found?

4

1 回答 1

10

删除以下内容map

'@angular/material': 'node_modules/@angular/material',

添加materialngPackageNames数组:

var ngPackageNames = [
 ...
 'material'
];

然后将您的packUmd功能替换为

function packUmd(pkgName) {
   packages['@angular/'+pkgName] = { 
     main: (pkgName !== 'material' ? 'bundles/' : '') + pkgName + '.umd.js', 
     defaultExtension: 'js' 
   };
}

之后它应该工作

或者像在 angular2 快速入门中一样使用这个配置:

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: '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',

      '@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
      '@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
      '@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
      '@angular/material': 'npm:@angular/material/bundles/material.umd.js'

      // other libraries
      'rxjs':                       'npm:rxjs'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);
于 2016-10-10T19:54:36.023 回答