簡體   English   中英

材料2-如何讓md-icon在SystemJS項目中工作?

[英]Material 2 - how to let md-icon work in SystemJS project?

有基於Material 2 SystemJS的項目。

物料2以這種方式安裝:

npm install --save @angular/material

然后將其連接到SystemJS配置中:

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

指定在index.html設置的圖標:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"/>

設置MdIconRegistry

...
import { MaterialModule } from '@angular/material';
import { MdIconRegistry } from '@angular/material/icon';

@NgModule({
    imports: [ BrowserModule, MaterialModule.forRoot() ],
    declarations: [ AppComponent ],
    providers: [ MdIconRegistry ],
    bootstrap: [ AppComponent ],
    schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule {
    constructor(mdIconRegistry: MdIconRegistry) {
        mdIconRegistry.registerFontClassAlias('material', 'material-icons');
    }
}

然后在AppComponent內部AppComponent

<md-icon fontSet="material">home</md-icon>

但是由於這些JS錯誤,它不起作用:

zone.js:1382 GET http://localhost:8080/node_modules/@angular/material/material.umd.js/icon 404 ()
Error: (SystemJS) XHR error (404) loading http://localhost:8080/node_modules/@angular/material/material.umd.js/icon

值得一提的是,相同的配置在使用Angular CLI進行的項目中效果很好。

如何讓md-icon在SystemJS項目中工作?

要使用默認圖標字體(與SystemJS一起使用),只需執行以下操作(它對我有用):

<md-icon>home</md-icon>

您無需根據docs使用MdIconRegistry:

在此處輸入圖片說明

順便說一句,您得到的錯誤是由以下行引起的:

import { MdIconRegistry } from '@angular/material/icon';

應該只是:

import { MdIconRegistry } from '@angular/material';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM