简体   繁体   English

如何在angular-cli中添加angular2材料?

[英]How to add angular2 material to angular-cli?

Trying to add angular materials to angular-cli but angular-materials never show up in vendor files. 试图向angular-cli中添加角度材料,但是角度材料永远不会出现在供应商文件中。 I added materials files to system-config.ts as shown below: 我将材料文件添加到system-config.ts中,如下所示:

      const barrels: string[] = [
     // Angular specific barrels.
      '@angular/core',
      '@angular/common',
      '@angular/compiler',
      '@angular/http',
      '@angular/router',
      '@angular/platform-browser',
      '@angular/platform-browser-dynamic',

       // Thirdparty barrels.
      'rxjs',

     './button/button.js',
    './card/card.js',
    './checkbox/checkbox.js',
    './input/input.js',
     './progress-circle/progress-circle.js',
     './sidenav/sidenav.js',
    './toolbar/toolbar.js',



        // App specific barrels.
         'app',
        'app/shared',
          /** @cli-barrel */
      ];

        const _cliSystemConfig = {};
       barrels.forEach((barrelName: string) => {
      _cliSystemConfig[barrelName] = { main: 'index' };
         });

            /** Type declaration for ambient System. */
               declare var System: any;

              // Apply the CLI SystemJS configuration.
               System.config({
              map: {
             '@angular': 'vendor/@angular',
             'rxjs': 'vendor/rxjs',
            'main': 'main.js',
            ' @angular2-material':'vendor/ @angular2-material'   
              },
          packages: _cliSystemConfig
                });

             // Apply the user's configuration.
           System.config({ map, packages });

Also I added to vedornpmfiles array in angular-cli-build.js as shown below: 另外,我在angular-cli-build.js中添加了vedornpmfiles数组,如下所示:

  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'

]

I am getting error that they cant find angular materials files. 我收到错误消息,他们找不到角形材料文件。

If anyone has got a clue. 如果有人知道。 What I am doing wrong? 我做错了什么?

Your system-config.ts should look like this: 您的system-config.ts应该如下所示:

/***********************************************************************************************
 * User Configuration.
 **********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
    '@angular2-material': 'vendor/@angular2-material',
};

/** User packages configuration. */
const packages: any = {
    '@angular2-material/core': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'core.js'
    },
    '@angular2-material/sidenav': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'sidenav.js'
    },
    '@angular2-material/toolbar': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'toolbar.js'
    },
    '@angular2-material/card': {
        format: 'cjs',
        defaultExtension: 'js',
        main: 'card.js'
    },

  // add missing material elements as desired 
};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
    // Angular specific barrels.
    '@angular/core',
    '@angular/common',
    '@angular/compiler',
    '@angular/http',
    '@angular/router',
    '@angular/router-deprecated',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',

    // Thirdparty barrels.
    'rxjs',

    // App specific barrels.
    'app',
    'app/shared',
    'app/imagecard-component',
  'app/search-component',
  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
    cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
    map: {
        '@angular': 'vendor/@angular',
        'rxjs': 'vendor/rxjs',
        'main': 'main.js'
    },
    packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

Note the map and packages entries. 注意地图和包裹条目。

For reference, see this sample app from one the angular2-material developers. 作为参考,请参阅angular2-material开发人员的示例应用程序

All these solutions have too much going on. 所有这些解决方案都在进行中。 First thing you should do is remove those barells that belong to Angular2-material. 您应该做的第一件事是删除属于Angular2材质的那些barells。

'./button/button.js',
'./card/card.js',
'./checkbox/checkbox.js',
'./input/input.js',
 './progress-circle/progress-circle.js',
 './sidenav/sidenav.js',
'./toolbar/toolbar.js',

in your system-config.ts add the angular 2 vendor path to the map object: 在system-config.ts中,将角度2供应商路径添加到地图对象:

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

Then here's the tricky bit, the Packages object is empty const packages: any {}; 然后是个棘手的问题,Packages对象是空的const packages: any {}; (unless you have already installed some third party packages and added them, yours will be too). (除非您已经安装了一些第三方软件包并添加了它们,否则您的软件包也将如此)。 So we have to declare the material packages we want to use, we do this by creating a array of the package names 因此,我们必须声明要使用的材料包装,我们通过创建包装名称数组来进行声明

const materialPkgs[]: string[]= [
    'core',
    'button'
];

In this case I'm only importing button and core for simplicity's sake. 在这种情况下,为简单起见,我仅导入按钮和核心 Core must always be imported the other modules depend on it. 必须始终导入 Core,其他模块也依赖于此。
Next we loop through these materialPkgs and add them to packages using a forEach function 接下来,我们遍历这些materialPkg,并使用forEach函数将它们添加到包中

materialPkgs.forEach((pkg) =>{
    packages[`@angular2-material/${pkg}`] = {main: `${pkg}.js`}
});  

we're almost done in system-config.ts, last thing we need to add the main vendor package to the map object of the system.config so it can be applied... 我们几乎已经在system-config.ts中完成了,最后,我们需要将主供应商软件包添加到system.config的map对象中,以便可以应用...

System.config({
    map: {
      '@angular': 'vendor/@angular',
      'rxjs': 'vendor/rxjs',
      'main': 'main.js',
      '@angular2-material': 'vendor/@angular2-material'
    },
    packages: cliSystemConfigPackages
});

Your angular-cli-build.js file is fine, if you configured the default extension to be 'js' like i did, and probably you did too, you don't need to add it to the vendorNpmFiles. 您的angular-cli-build.js文件很好,如果您像我一样将默认扩展名配置为'js',并且可能也这样做,则无需将其添加到vendorNpmFiles中。 You could just as simply have '@angular2-material/**/*' 您可以简单地使用'@angular2-material/**/*'

And we're done with the configuration. 至此我们完成了配置。

import the directives in your component 将指令导入您的组件中
import { MD_BUTTON_DIRECTIVES } from '@angular2-material/button';

add them to your directives array 将它们添加到指令数组
directives: [ MD_BUTTON_DIRECTIVES ]

and use them in your markup 并在标记中使用它们

<h1>
  {{title}}
</h1>
<button md-button>FLAT</button>
<button md-raised-button>RAISED</button>
<button md-fab>add</button>
<button md-mini-fab>add</button>
<button md-raised-button color="primary">PRIMARY</button>
<button md-raised-button color="accent">ACCENT</button>
<button md-raised-button color="warn">WARN</button>  

They should work. 他们应该工作。

Screenshot of app working: 应用程序工作的屏幕截图:

正在运行的应用程序的屏幕截图

I had the same problem with "@angular/http" . 我对“ @ angular / http”有同样的问题。 So The way I fixed the problem ,it might help you too - 所以我解决问题的方式可能也对您有帮助-

Open your package.json and add this line under the dependencies - 打开package.json并在依赖项下添加以下行-

'@angular2-material': "your version"

Then open your terminal on that folder and type - 然后在该文件夹中打开终端,然后输入-

npm install

It will update your system-config.ts with 它将使用以下命令更新您的system-config.ts

const barrels: string[] = [
     // Angular specific barrels.
      '@angular/core',
      '@angular/common',
      '@angular2-material',
      '@angular/compiler',
      '@angular/http',
      '@angular/router',
      '@angular/platform-browser',
      '@angular/platform-browser-dynamic',

       // Thirdparty barrels.
      'rxjs',

     './button/button.js',
    './card/card.js',
    './checkbox/checkbox.js',
    './input/input.js',
     './progress-circle/progress-circle.js',
     './sidenav/sidenav.js',
    './toolbar/toolbar.js',

         'app',
        'app/shared',
          /** @cli-barrel */
      ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM