1

我有一个加载 Angular 2.0.0 的应用程序,即 2016 年 9 月 14 日发布的所有 rc 版本之后。我正在尝试添加 Google 团队的官方 MD 版本 Angular Material2。我正在从他们的入门站点复制/粘贴代码,除了我使用的是 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 Not Found) loading 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 标签。

我应该改变什么?

4

2 回答 2

2

今天早上(9/16)我能够使用 Angular2(最终版)、Angular2-Material 2.0.0-alpha.8-2 和 TypeScript 2.0.0 让它工作。正如您在下面的 package.json 中看到的那样,我按照此视频中的说明安装(并运行)了大多数当前可用的 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.

于 2016-09-16T13:55:09.133 回答
1

随着 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()],
  ...
})

材料 2 发布

Angular Material 已从 @angular2-material/... 包更改为 @angular/material 下的单个包。随着这个变化,还有一个新的 NgModule,MaterialModule,它包含了所有的组件。...

于 2016-11-01T06:15:50.753 回答