如何使用angular2-material with angular2

时间:2016-07-17 06:56:02

标签: angular npm angular2-material

注意:我不使用Angular Cli。

我添加了 @ angular2-material / core 包。然后我安装了其他angular2材质组件,如 @ angular2-material / button @ angular2-material / card 等等。

systemjs.config.js

/**
 * 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',
    '@angular2-material':         'node_modules/@angular2-material', 
    // <--------------added this above line----------------------
  };
  // 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',
  ];

  //<------------------added this below part--------------------------------
var materialPackages=[
    'core',
    //'button',
    //'card',
    'list',
  ];
  materialPackages.forEach(function(pkgName) {
    packages['@angular2-material/'+pkgName] = { format: 'cjs', defaultExtension: 'js'};
  });
 //<----------------------till here---------------------------------


  // 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);

当我运行我的应用时,我会看到以下错误,

  

无法加载资源:服务器响应状态为404   (未找到)   http://localhost:3000/node_modules/@angular2-material/sidenav/失败   加载资源:服务器响应状态为404(不是   找到了)http://localhost:3000/node_modules/@angular2-material/toolbar/   无法加载资源:服务器响应状态为404   (未找到)localhost /:16错误:错误:XHR错误(未找到404)   装载   http://localhost:3000/node_modules/@angular2-material/button(...)(匿名   功能)@ localhost /:16   http://localhost:3000/node_modules/@angular2-material/card/失败了   加载资源:服务器响应状态为404(未找到)   http://localhost:3000/node_modules/@angular2-material/input/失败了   加载资源:服务器响应状态为404(未找到)   http://localhost:3000/node_modules/@angular2-material/checkbox/失败   加载资源:服务器响应状态为404(不是   找到了)http://localhost:3000/node_modules/@angular2-material/radio/   无法加载资源:服务器响应状态为404   (未找到)   http://localhost:3000/node_modules/@angular2-material/icon/失败了   加载资源:服务器响应状态为404(未找到)   http://localhost:3000/node_modules/@angular2-material/list/失败了   加载资源:服务器响应状态为404(未找到)   http://localhost:3000/favicon.ico无法加载资源:服务器   回复状态为404(未找到)

3 个答案:

答案 0 :(得分:1)

您应该在system-config.ts

中根据需要单独添加材料组件

1。 angular-cli定位dist/目录

const map: any = {
  '@angular2-material/core': 'vendor/@angular2-material/core'
};

const packages: any = {
  '@angular2-material/core': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'core.js'
  }
};

angular-cli-build.js

vendorNpmFiles: [
  'systemjs/dist/system-polyfills.js',
  'systemjs/dist/system.src.js',
  'zone.js/dist/*.js',
  'es6-shim/es6-shim.js',
  'reflect-metadata/*.js',
  'rxjs/**/*.js',
  '@angular/**/*.js',
  '@angular2-material/**/*.js'
]

2。 lite-server或http-server目标应用程序根目录

const map: any = {
  '@angular2-material/core': 'node_modules/@angular2-material/core'
};

const packages: any = {
  '@angular2-material/core': {
    format: 'cjs',
    defaultExtension: 'js',
    main: 'core.js'
  }
};

答案 1 :(得分:0)

以下是角度材料2的来源system.config.ts

material2/GETTING_STARTED.md

npm

plunker

npm i @angular2-material/core --save

npm i @angular2-material/card --save

npm i @angular2-material/toolbar --save

npm i @angular2-material/button --save

npm i @angular2-material/slide-toggle --save

适用于:Angular 2 QuickStart Source

组件:

import { Component } from '@angular/core';
import { MD_SLIDE_TOGGLE_DIRECTIVES } from '@angular2-material/slide-toggle';
import { MD_TOOLBAR_DIRECTIVES } from '@angular2-material/toolbar';

import { MD_CARD_DIRECTIVES } from '@angular2-material/card';
import { MD_BUTTON_DIRECTIVES } from '@angular2-material/button';
@Component({
    selector: 'my-app',
    directives: [MD_TOOLBAR_DIRECTIVES,MD_CARD_DIRECTIVES,MD_SLIDE_TOGGLE_DIRECTIVES, MD_BUTTON_DIRECTIVES],

    template: `

            <h1>
        {{title}}
        </h1>
        <md-toolbar color="primary">
            Angular Material 2 App
        </md-toolbar>

        <div style="padding: 7px">
            <button md-button>Basic Button</button>
            <button md-raised-button>Raised Button</button>

            <md-slide-toggle>Slide Toggle</md-slide-toggle>
        </div>

            `
})
export class AppComponent { }

系统配置:

    (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',

            '@angular2-material/core': 'node_modules/@angular2-material/core',
            '@angular2-material/button': 'node_modules/@angular2-material/button',
            '@angular2-material/card': 'node_modules/@angular2-material/card',
            '@angular2-material/slide-toggle': 'node_modules/@angular2-material/slide-toggle',
            '@angular2-material/toolbar': 'node_modules/@angular2-material/toolbar'


        };

        // 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'
            },



            '@angular2-material/core': {
                main: 'core.js'
            },
            '@angular2-material/card': {
                main: 'card.js'
            },
            '@angular2-material/button': {
                main: 'button.js'
            },
            '@angular2-material/toolbar': {
                main: 'toolbar.js'
            },
            '@angular2-material/slide-toggle': {
                main: 'slide-toggle.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);

        // No umd for router yet
        packages['@angular/router'] = {
            main: 'index.js',
            defaultExtension: 'js'
        };

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

        System.config(config);

    })(this);

答案 2 :(得分:0)

请将@ angular2 RC4更新为RC5并添加如下配置文件

UIStackView