3

我最近将我的项目升级到 Angular 2 RC 6 和 Material 2.0.0-alpha.8-1 但它不起作用,谁能指导我在这里缺少什么?任何人都可以与 Angular 2 RC 6 和 Material 2.0.0-alpha.8-1 分享一个工作的 plunker 吗?

我的 package.json 中有以下软件包:

"@angular2-material/button": "2.0.0-alpha.8-1",
"@angular2-material/button-toggle": "2.0.0-alpha.8-1",
"@angular2-material/card": "2.0.0-alpha.8-1",
"@angular2-material/checkbox": "2.0.0-alpha.8-1",
"@angular2-material/core": "2.0.0-alpha.8-1",
"@angular2-material/grid-list": "2.0.0-alpha.8-1",
"@angular2-material/icon": "2.0.0-alpha.8-1",
"@angular2-material/input": "2.0.0-alpha.8-1",
"@angular2-material/list": "2.0.0-alpha.8-1",
"@angular2-material/menu": "2.0.0-alpha.8-1",
"@angular2-material/progress-bar": "2.0.0-alpha.8-1",
"@angular2-material/progress-circle": "2.0.0-alpha.8-1",
"@angular2-material/radio": "2.0.0-alpha.8-1",
"@angular2-material/sidenav": "2.0.0-alpha.8-1",
"@angular2-material/slider": "2.0.0-alpha.8-1",
"@angular2-material/slide-toggle": "2.0.0-alpha.8-1",
"@angular2-material/tabs": "2.0.0-alpha.8-1",
"@angular2-material/toolbar": "2.0.0-alpha.8-1",
"@angular2-material/tooltip": "2.0.0-alpha.8-1",

在 AppModule 中,我首先导入了以下所需模块:

import { MdCoreModule } from '@angular2-material/core/core';
import { MdCheckboxModule } from '@angular2-material/checkbox/checkbox';
import { MdProgressBarModule } from '@angular2-material/progress-bar/progress-bar';
import { MdRadioModule, MdUniqueSelectionDispatcher } from '@angular2-material/radio/radio';
import { MdSlideToggleModule } from '@angular2-material/slide-toggle/slide-toggle';
import { MdTabsModule } from '@angular2-material/tabs/tabs';

然后在导入列表中添加以下内容:

imports: [
    ....
    // Material Design
    MdCoreModule.forRoot(),
    MdCheckboxModule.forRoot(),
    MdRadioModule.forRoot(),
    MdSlideToggleModule.forRoot(),
    MdTabsModule.forRoot(),
    MdProgressBarModule.forRoot(),
    ....
],

在提供者中:

providers: [
        MdUniqueSelectionDispatcher
]

在我的 SystemJs 配置文件中,我有:

var map = {
    '@angular': 'node_modules/@angular',
    '@angular2-material': 'node_modules/@angular2-material',

    'rxjs': 'node_modules/rxjs',

    'app': 'content/app',
    "angular2-masonry": "node_modules/angular2-masonry"
}; 

var paths = {
        "masonry-layout": "node_modules/masonry-layout/dist/masonry.pkgd.js"
    };

var packages = {
        'app': { main: 'main.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        'angular2-masonry': { defaultExtension: 'js', main: "index" }
    };


var angularPackages = [
    'common',
    'compiler',
    'core',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router',
    'forms'
];

angularPackages.forEach(function (name) {
    packages['@angular/' + name] = {
        format: 'cjs',
        main: 'bundles/' + name + '.umd.js', defaultExtension: 'js'
    };
});

var materialComponents = [
        'core',
        'button',
        'card',
        'checkbox',
        'grid-list',
        'icon',
        'input',
        'list',
        'progress-bar',
        'progress-circle',
        'radio',
        'sidenav',
        'slide-toggle',
        'tabs',
        'toolbar'
    ];

    materialComponents.forEach(function (name) {
        packages[("@angular2-material/" + name)] = {
            format: 'cjs',
            main: name + '.umd.js',
            defaultExtension: 'js'
        };
    });

    var config = {
        map: map,
        packages: packages,
        paths: paths
    };

    System.config(config);

以下是我运行应用程序时控制台上显示的错误消息:

43 Error: SyntaxError: Unexpected token import
at Object.eval (http://localhost:57196/content/app/app.module.js:16:14)
at eval (http://localhost:57196/content/app/app.module.js:328:4)
at eval (http://localhost:57196/content/app/app.module.js:329:3)
Evaluating http://localhost:57196/node_modules/@angular2-material/core/core.js
Evaluating http://localhost:57196/content/app/app.module.js
Evaluating http://localhost:57196/content/app/main.js
Error loading http://localhost:57196/content/app/main.js
4

2 回答 2

4

搞定了!如果有人仍在努力让 Angular 2 RC 6 Material 2 Alpha 8 在您的项目中运行,那么下面的配置可能会起作用。

下面是我的 SystemJS 配置文件

(function (global) {
    var map = {
        //angular
        '@angular': 'node_modules/@angular',
        '@angular2-material': 'node_modules/@angular2-material',
        'rxjs': 'node_modules/rxjs',

        'app': 'content/app',

        //thirdparty
        "angular2-masonry": "node_modules/angular2-masonry",
    };

    var paths = {
        "masonry-layout": "node_modules/masonry-layout/dist/masonry.pkgd.js"
    };

    var packages = {
        'app': { main: 'main.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        "angular2-masonry": { defaultExtension: 'js', main: "index" }
    };

    var angularPackages = [
        'common',
        'compiler',
        'core',
        'http',
        'platform-browser',
        'platform-browser-dynamic',
        'router',
        'forms'
    ];

    angularPackages.forEach(function (name) {
        packages['@angular/' + name] = {
            format: 'cjs',
            main: 'bundles/' + name + '.umd.js',
            defaultExtension: 'js'
        };
    });

    var materialComponents = [
        'checkbox',
        'core',
        'progress-bar',
        'progress-circle',
        'radio',
        'sidenav',
        'slide-toggle',
        'tabs'
    ];

    materialComponents.forEach(function (name) {
        packages[("@angular2-material/" + name)] = {
            format: 'cjs',
            main: name + '.umd.js',
            defaultExtension: 'js'
        };
    });

    var config = {
        map: map,
        packages: packages,
        paths: paths
    };

    System.config(config);

})(this);

应用模块

进口

//angular
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Http, XHRBackend, HttpModule, RequestOptions, JsonpModule } from '@angular/http';
import { NgModule } from '@angular/core';

//material
import { MdCheckboxModule } from '@angular2-material/checkbox';
import { MdProgressBarModule } from '@angular2-material/progress-bar';
import { MdRadioModule, MdUniqueSelectionDispatcher } from '@angular2-material/radio';
import { MdSlideToggleModule } from '@angular2-material/slide-toggle';
import { MdTabsModule } from '@angular2-material/tabs';

NgModule 中的导入

@NgModule({
    imports: [
        //angular
        BrowserModule,
        FormsModule,
        HttpModule,
        JsonpModule,
        ReactiveFormsModule,
        routing,

        //material
        MdCheckboxModule.forRoot(),
        MdRadioModule.forRoot(),
        MdSlideToggleModule.forRoot(),
        MdTabsModule.forRoot(),
        MdProgressBarModule.forRoot(),
        MdProgressBarModule.forRoot()
//.....
    ],

如果您使用收音机

providers: [
        MdUniqueSelectionDispatcher
]
于 2016-09-07T01:15:36.627 回答
1

我将材料 2 与“官方”systemjs.config.js文件一起使用,但无法使其与.js桶一起使用。

所以我继续将它们映射到.umd文件,例如

map: {
    ...
'@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',
    ...
}

并删除了该部分中的相关条目packages。这对我有用。

于 2016-09-10T22:59:22.160 回答