【问题标题】:How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app如何将 angular2-material (alpha 8.2) 与 angular2-Quickstart 应用程序集成
【发布时间】:2017-01-26 05:41:25
【问题描述】:

注意:我不使用 Angular-CLI

这个 plunker 正在使用 Alpha-8.1https://plnkr.co/edit/qoZ3YCwSz0mQ5o974Dt0?p=preview


我的快速启动应用正在运行,没有任何问题。

第 1 步:我更新了 package.json 以包含 angular2-materialangular2-button 包。

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"
  }
}

第 2 步:我运行了 npm install,我可以看到 node_modules 文件夹下的 @angular-material 文件夹.


第 3 步:更新 systemjs.config.js 以将 @angular2/material 包映射到 node_modules umd.js 文件。

注意我有 app: 'dist' 来分隔 .map.js.js 来自 .ts 文件的文件

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

第 4 步:更新 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 { }

第 5 步:更新 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 { }

错误

localhost/:16 错误:错误:XHR 错误(404 未找到)正在加载 http://localhost:3000/node_modules/@angular2-material/button/button.umd.js(…)

【问题讨论】:

  • 您确定您的node_modules/@angular2-material/button/button.umd.js 路径存在吗?你还有一个错字&lt;button md-button-raised&gt;Button&lt;/button&gt;。应该是 &lt;button md-raised-button&gt;Button&lt;/button&gt; 我在你的 AppModule 中看到了双重导入
  • 等一下,让我检查一下。
  • 是的,一切都如前所述。
  • 这只是因为双 imports 而已。鹰眼@yurzui。我正在做一些实验,所以忘记删除它。一切都按预期工作。
  • 很高兴听到:)

标签: angular systemjs angular2-material


【解决方案1】:

注意将 angular2-material 与核心 angular2 应用程序集成,而不是与 Angular2-CLI 集成。

我有一个小错字,已更正,所以现在一切正常。

注意:那些在将 Angular2-material核心 Angular2 应用程序 集成时遇到一些障碍的人,可以按照问题本身建议的步骤进行操作。

我已经在早期版本中问过这个问题两次,但没有任何效果。

但最后,通过以下步骤,您将能够将 angular2-materialcore angular2 app 一起使用。

【讨论】:

    【解决方案2】:

    我尝试了更多的解决方法,允许您使用按钮和东西而无需在 systemjs.config.js 文件中单独导入

    第一步:创建 package.json 文件

    {
      "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": {}
    }
    

    第二步:使用 npm 命令 npm install

    第 3 步: 创建 systemjs.config.js 文件

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

    第五步:使用 npm 命令 npm install @angular/material

    第 6 步: 在 app.module.ts 文件中添加以下内容

    import { NgModule } from '@angular/core';
    import { FormsModule } from '@angular/forms';
    import { MaterialModule } from '@angular/material';
    
    @NgModule({
        imports: [
            FormsModule,
            MaterialModule.forRoot(),
           ])
    export class AppModule { }
    

    第 7 步:检查项目中的以下文件夹

    1. node_modules
    2. node_module/@angulae/material
    3. node_modules/angular-material
    4. 打字(如果不可用,请使用 npm)

    它将开始工作

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 2016-12-12
      • 2016-05-03
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2016-07-10
      • 2018-10-31
      • 2017-06-10
      相关资源
      最近更新 更多