How to integrate angular2-material (alpha 8.2) wit

2019-06-07 21:41发布

NOTE: I don't use Angular-CLI

This plunker is working with Alpha-8.1 : https://plnkr.co/edit/qoZ3YCwSz0mQ5o974Dt0?p=preview


I have my quick-start app running without any issue.

Step 1: I updated package.json to include angular2-material and angular2-button packages.

package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    ...
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "systemjs": "0.19.27",
    ...
    ...

    //<<<<<<<<<<<<<<<<<<<<<Here>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    "@angular2-material/core":"2.0.0-alpha.8-2",      
    "@angular2-material/button ":"2.0.0-alpha.8-2"     
    //<<<<<<<<<<<<<<<<<<<<<Here>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.2",
    "typings":"^1.3.2"
  }
}

Step 2: I ran npm install, I could see @angular-material folder under node_modules folder.


Step 3: Updated systemjs.config.js to map @angular2/material package to node_modules umd.js file.

NOTE : I have app: 'dist' to separate .map.js and .js files from .ts files

systemjs.config.js

(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: 'dist',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      ...
      ...
      //<<<<<<<<<<<<<<<<<<<<<Here>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      '@angular2-material/core': 'npm:@angular2-material/core/core.umd.js',
      '@angular2-material/button': 'npm:@angular2-material/button/button.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);

Step 4: Updated app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { MdButtonModule } from '@angular2-material/button';
//import { MdCardModule } from '@angular2-material/card';
@NgModule({
  imports:      [ BrowserModule,MdButtonModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Step 5: Updated app.component.ts

import { Component } from '@angular/core';
import { MD_BUTTON_DIRECTIVES } from '@angular2-material/button';
@Component({
  selector: 'my-app',
  template: `<h1>My First Angular 2 App Live</h1>
  <button md-raised-button>Button</button>
  `
})
export class AppComponent { }

Error:

localhost/:16 Error: Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/@angular2-material/button/button.umd.js(…)

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-07 21:43

NOTE: integration of angular2-material with core angular2 app, not with Angular2-CLI.

I had a small typo, corrected it so now everything is working.

NOTE: Those who are facing some hurdles in integrating Angular2-material with core Angular2 app, can follow steps suggested in question itself.

I had already asked this question two times with earlier releases but nothing was working.

But finally, by following steps shown in question, you will be able to use angular2-material with core angular2 app.

查看更多
我想做一个坏孩纸
3楼-- · 2019-06-07 21:46

I have tried with on more workaround which allow your to use button and stuff without importing separately in systemjs.config.js file

Step 1: Create package.json file

{
  "name": "fist-angular-app",
  "version": "1.0.0",
  "author": "any name you want",
  "description": "this is the first angular app",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.2.0",
    "@angular/compiler": "2.2.0",
    "@angular/core": "2.2.0",
    "@angular/forms": "2.2.0",
    "@angular/http": "2.2.0",
    "@angular/platform-browser": "2.2.0",
    "@angular/platform-browser-dynamic": "2.2.0",
    "@angular/router": "3.0.0",
    "angular-material": "^1.1.1",

    "bootstrap": "^3.3.6",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "jquery": "^3.1.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@types/hammerjs": "^2.0.33",
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.0",
    "tslint": "^3.7.4",
    "typescript": "^2.0.2",
    "typings": "^1.0.4"
  },
  "repository": {}
}

Step 2: Use npm Command npm install

Step 3: Create systemjs.config.js file

Step 4:

/**
 * 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',
      '@angular/material': 'npm:@angular/material/material.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs'
    },
    // 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'
      }
    }
  });
})(this);

Step 5: Use npm Command npm install @angular/material

Step 6: Add below in app.module.ts file

import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MaterialModule } from '@angular/material';

@NgModule({
    imports: [
        FormsModule,
        MaterialModule.forRoot(),
       ])
export class AppModule { }

Step 7: Check Below Folders in Project

  1. node_modules
  2. node_module/@angulae/material
  3. node_modules/angular-material
  4. typing (if not available use npm for this)

And it will start working

查看更多
登录 后发表回答