2016-09-23 174 views
0

我正在嘗試將角材2導入到一個簡單的Angular 2應用中。我已經通過NPM成功下載了這些文件,並且可以將MdCardModule導入到我的app.module.ts中,並且無任何錯誤地進行編譯。問題是,只要我將MdCardModule作爲@NgModule的一部分包含進來,而不是將組件文本「WORKING」顯示爲空白。任何理由?無法加載角材2模塊

這裏是我的代碼:

app.component.ts

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
    template: '<h1>WORKING</h1>' 
}) 
export class AppComponent { } 

app.module.ts

import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { MdCoreModule} from '@angular2-material/core'; 
import { MdCardModule} from '@angular2-material/card'; 


import { AppComponent } from './app.component'; 

@NgModule({ 
    imports:  [ BrowserModule,MdCoreModule.forRoot(),MdCardModule.forRoot()], 
    declarations: [ AppComponent ], 
    bootstrap: [ AppComponent ], 

}) 

export class AppModule { } 

的index.html

<html> 
    <head> 
    <title>Angular 2 QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="styles.css"> 
    <!-- 1. Load libraries --> 
    <!-- Polyfill(s) for older browsers --> 
    <script src="node_modules/core-js/client/shim.min.js"></script> 
    <script src="node_modules/zone.js/dist/zone.js"></script> 
    <script src="node_modules/reflect-metadata/Reflect.js"></script> 
    <script src="node_modules/systemjs/dist/system.src.js"></script> 
    <!-- 2. Configure SystemJS --> 
    <script src="systemjs.config.js"></script> 
    <script> 
     System.import('app').catch(function(err){ console.error(err); }); 
    </script> 
    <!-- Load the core.css stylesheet manually (temporary) --> 
    <link href="https://npmcdn.com/@angular2-material/[email protected]/style/core.css"> 
    </head> 
    <!-- 3. Display the application --> 
    <body> 
    <my-app></my-app> 

    </body> 
</html> 

回答

0

您還需要導入MdCoreModule.forRoot()和MdCoreModul e.forRoot()在你的根模塊中。

+0

感謝您的回覆,我試過仍然沒有運氣。代碼如上所述。很抱歉誤讀你的訊息。將嘗試您的建議 –

+0

沒有運氣,與mdcore –

+0

我幾乎沒有與SystemJS expierence,但我建議你嘗試官方'angular-cli'與webpack。運行起來要容易得多。 –

0

我有同樣的問題,並能解決它編輯systemjs.config.js

/** 
* System configuration for Angular 2 samples 
* Adjust as necessary for your application needs. 
*/ 
(function (global) { 
    var packages = { 
      app: { main: './main.js', defaultExtension: 'js' }, 
      rxjs: { defaultExtension: 'js'}, 
      'angular2-in-memory-web-api': { main: './index.js', defaultExtension: 'js' }, 
     }, 
     materialPackages = [ 'core', 'button', 'card', 'toolbar' ]; 

    materialPackages.forEach(function(pkgName) { 
     packages[`@angular2-material/${pkgName}`] = { main: `${pkgName}.umd.js`, defaultExtension: 'js', format: 'cjs' }; 
    }); 

    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', 
      // other libraries 
      'rxjs':      'npm:rxjs', 
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api', 
      // @angular2-material 
      '@angular2-material': 'npm:@angular2-material', 
     }, 
     // packages tells the System loader how to load when no filename and/or no extension 
     packages: packages 
    }); 
})(this); 

而且在app.module.ts我還沒有進口MdCoreModule