简体   繁体   中英

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

Have Material 2 SystemJS based project.

Material 2 is installed in this way:

npm install --save @angular/material

Then it's wired up in SystemJS config:

...
'@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',
...

Specifying the icon set in index.html :

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

Setting up the 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');
    }
}

Then it's used inside AppComponent :

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

But it doesn't work due these JS errors:

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

Worth to say, the same configuration works fine in the project made using Angular CLI.

How to let the md-icon work in SystemJS project?

To use the default icon font (with SystemJS) just do the following (it works for me):

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

You don't need to use MdIconRegistry according to the docs :

在此处输入图片说明

As an aside, the error you are getting is caused by this line:

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

It should just be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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