angular material2 在 Angular 2.0.0 应用程序中不起作用

angular material2 does not work in an Angular 2.0.0 app

我有一个应用程序加载了 Angular 2.0.0,即所有 rc 版本之后的 9/14/16 版本。我正在尝试添加 Angular Material2,这是 Google 团队的官方 MD 版本。我是来自他们入门网站的 copy/pasting 代码,除了我使用的是 Atom 而不是 angular-cli,因为我已经构建了应用程序的开头部分。 Atom-typescript 找到所有内容并说没有错误。但是尽管经过大量试验,当修改后的应用程序加载失败时,我反复遇到这些未找到的控制台错误。

zone.js:1263 GET http://localhost:3000/node_modules/@angular2-material/button/ 404(未找到)scheduleTask @ zone.js:1263

home:27 错误:(SystemJS) XHR 错误(404 未找到)加载 http://localhost:3000/node_modules/@angular2-material/button(…)

zone.js:1263 GET http://localhost:3000/node_modules/@angular2-material/list/ 404(未找到)

我查看了上面的 node_modules 文件,它们存在于我的代码中。

根据入门指南中的说明,相关代码为:

app.module.ts

import { MdButtonModule } from '@angular2-material/button';
import { MdListModule } from '@angular2-material/list';
@NgModule({
  imports:      [ BrowserModule,
                  FormsModule,
                  MdButtonModule,
                  MdListModule,
                  routing
  ],

systemjs.config.js

var map = {
'@angular2-material':                   'node_modules/@angular2-material',
'@angular2-material/core':                   'node_modules/@angular2-material/core/core',
'@angular2-material/list':                   'node_modules/@angular2-material/list',
'@angular2-material/button':                   'node_modules/@angular2-material/button'
};
var packages = {
'app':                        { main: 'main.js',  defaultExtension: 'js' },
'rxjs':                       { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
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);

HTML 页面有几个 md-list、md-list-item 和 md-button 标签。

我应该改变什么?

今天早上(9 月 16 日)我能够使用 Angular2(最终版)、Angular2-Material 2.0.0-alpha.8-2 和 TypeScript 2.0.0 让它工作。正如您在下面的 package.json 中看到的,我按照 this video 中的说明安装了大多数当前可用的 Angular2 Material 组件(和 运行)。

发布我的 system.config.ts 以便您查看,希望它能帮助您解决问题。

/**
 * System configuration for Angular 2 samples
 * Adjust as necessary for your application needs.
 */
(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',

      // other libraries
      'rxjs':                       'npm:rxjs',
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',

      // Angular2-Material
      '@angular2-material/core': 'npm:@angular2-material/core/core.umd.js',
      '@angular2-material/button': 'npm:@angular2-material/button/button.umd.js',
      '@angular2-material/card': 'npm:@angular2-material/card/card.umd.js',
      '@angular2-material/checkbox': 'npm:@angular2-material/checkbox/checkbox.umd.js',
      '@angular2-material/grid-list': 'npm:@angular2-material/grid-list/grid-list.umd.js',
      '@angular2-material/icon': 'npm:@angular2-material/icon/icon.umd.js',
      '@angular2-material/input': 'npm:@angular2-material/input/input.umd.js',
      '@angular2-material/list': 'npm:@angular2-material/list/list.umd.js',
      '@angular2-material/radio': 'npm:@angular2-material/radio/radio.umd.js',
      '@angular2-material/sidenav': 'npm:@angular2-material/sidenav/sidenav.umd.js',
      '@angular2-material/toolbar': 'npm:@angular2-material/toolbar/toolbar.umd.js',
    },
    // 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'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

我package.json的相关部分:

      ...   
      "dependencies": {
        "@angular/common": "~2.0.0",
        "@angular/compiler": "~2.0.0",
        "@angular/core": "~2.0.0",
        "@angular/forms": "~2.0.0",
        "@angular/http": "~2.0.0",
        "@angular/platform-browser": "~2.0.0",
        "@angular/platform-browser-dynamic": "~2.0.0",
        "@angular/router": "~3.0.0",
        "@angular/upgrade": "~2.0.0",
        "@angular2-material/button": "^2.0.0-alpha.8-2",
        "@angular2-material/card": "^2.0.0-alpha.8-2",
        "@angular2-material/checkbox": "^2.0.0-alpha.8-2",
        "@angular2-material/core": "^2.0.0-alpha.8-2",
        "@angular2-material/grid-list": "^2.0.0-alpha.8-2",
        "@angular2-material/icon": "^2.0.0-alpha.8-2",
        "@angular2-material/input": "^2.0.0-alpha.8-2",
        "@angular2-material/list": "^2.0.0-alpha.8-2",
        "@angular2-material/radio": "^2.0.0-alpha.8-2",
        "@angular2-material/sidenav": "^2.0.0-alpha.8-2",
        "@angular2-material/toolbar": "^2.0.0-alpha.8-2",
        "angular2-in-memory-web-api": "~0.0.19",
        "core-js": "^2.4.1",
        "hammerjs": "^2.0.8",
        "reflect-metadata": "^0.1.3",
        "rxjs": "~5.0.0-beta.12",
        "systemjs": "~0.19.27",
        "zone.js": "^0.6.21"
      },
      "devDependencies": {
        "concurrently": "^2.2.0",
        "lite-server": "^2.2.2",
        "typescript": "^2.0.0",
        "typings": "^1.3.2"
      }
      ...

已将打字稿更新至 2.0.0 版。

还必须为 hammerjs 注册类型(触摸手势支持),但我相信只有在使用 MdIconModule 时才需要它。

随着 Angular Material 2 版本 2.0.0-alpha.9 的重大更改,您可以简单地将这一行映射到 systemjs.config.ts

'@angular/material': 'npm:@angular/material/material.umd.js',

package.json 也变得更简单:

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

并且不要忘记导入 NgModule

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

来自material2 release

Angular Material has changed from @angular2-material/... packages to a single package under @angular/material. Along with this change, there is a new NgModule, MaterialModule, that contains all of the components. ...