使用mdIcon时从控制台获取错误

时间:2016-05-09 14:37:51

标签: angular typescript angular-material

当我使用mdIcon时,Angular会抛出错误。以下是代码:

import {Component} from '@angular/core';
import {MdIcon} from '@angular2-material/icon';
@Component({
    selector: 'my-app',
    template: `
    <md-toolbar>
      <md-icon class="demo-toolbar-icon">menu</md-icon>
      <span>Default Toolbar</span>

      <span class="demo-fill-remaining"></span>

      <md-icon>code</md-icon>
    </md-toolbar>`,
    directives: [MdIcon,MdToolbar],

})
export class AppComponent {} 

错误:

  

原始例外:没有MdIconRegistry的提供商!   错误:DI例外           at NoProviderError.BaseException [as constructor](exceptions.ts:14)           在NoProviderError.AbstractProviderError [作为构造函数](reflective_exceptions.ts:53)           在新的NoProviderError(reflective_exceptions.ts:85)           在ReflectiveInjector _._ throwOrNull(reflective_injector.ts:844)           在ReflectiveInjector _._ getByKeyDefault(reflective_injector.ts:873)           在ReflectiveInjector _._ getByKey(reflective_injector.ts:835)           在ReflectiveInjector_.get(reflective_injector.ts:632)           在ElementInjector.get(element_injector.ts:19)           在DebugAppView._View_AppComponent0.createInternal(AppComponent.template.js:141)           在DebugAppView.AppView.create(view.ts:110)

任何人都可以帮助我,我在这里做错了吗?

5 个答案:

答案 0 :(得分:12)

使用材料alpha 8-4更新2.0.0版本:

您需要在app.modules.ts文件中包含MdIconRegistry作为提供者。这是一个功能性的例子:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { MdButtonModule } from '@angular2-material/button';
import { MdButtonToggleModule } from '@angular2-material/button-toggle';
import { MdCardModule } from '@angular2-material/card';
import { MdCheckboxModule } from '@angular2-material/checkbox';
import { MdGridListModule } from '@angular2-material/grid-list';
import { MdIconModule, MdIconRegistry } from '@angular2-material/icon';
import { MdInputModule } from '@angular2-material/input';
import { MdListModule } from '@angular2-material/list';
import { MdMenuModule } from '@angular2-material/menu';
import { MdProgressBarModule } from '@angular2-material/progress-bar';
import { MdProgressCircleModule } from '@angular2-material/progress-circle';
import { MdRadioModule } from '@angular2-material/radio';
import { MdSidenavModule } from '@angular2-material/sidenav';
import { MdSliderModule } from '@angular2-material/slider';
import { MdSlideToggleModule } from '@angular2-material/slide-toggle';
import { MdTabsModule } from '@angular2-material/tabs';
import { MdToolbarModule } from '@angular2-material/toolbar';
import { MdTooltipModule } from '@angular2-material/tooltip';

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


@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,

        MdButtonModule,
        MdButtonToggleModule,
        MdCardModule,
        MdCheckboxModule,
        MdGridListModule,
        MdIconModule,
        MdInputModule,
        MdListModule,
        MdMenuModule,
        MdProgressBarModule,
        MdProgressCircleModule,
        MdRadioModule,
        MdSidenavModule,
        MdSliderModule,
        MdSlideToggleModule,
        MdTabsModule,
        MdToolbarModule,
        MdTooltipModule
    ],
    providers: [
        MdIconRegistry
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

如果要使用md-slide-toggle和md-slider模块,请不要忘记安装hammerjs + typings并将import 'hammerjs';添加到main.ts文件中。

答案 1 :(得分:7)

您需要在providersviewProviders中列出并注册一些图标或图标集

import {Component, ViewEncapsulation} from '@angular/core';
import {MdIcon} from '@angular2-material/icon';

@Component({
    selector: 'my-app',
    template: `
    <md-toolbar>
      <md-icon class="demo-toolbar-icon">menu</md-icon>
      <span>Default Toolbar</span>

      <span class="demo-fill-remaining"></span>

      <md-icon>code</md-icon>
    </md-toolbar>`,
    directives: [MdIcon,MdToolbar],
    viewProviders: [MdIconRegistry],
})
export class AppComponent {
    constructor(mdIconRegistry: MdIconRegistry) {
        mdIconRegistry
            .addSvgIcon('thumb-up', '/demo-app/icon/assets/thumbup-icon.svg')
            .addSvgIconSetInNamespace('core', '/demo-app/icon/assets/core-icon-set.svg')
            .registerFontClassAlias('fontawesome', 'fa');
    }
}

答案 2 :(得分:2)

您应该只能在app.module.ts中导入MaterialRootModule:

import { MaterialModule } from '@angular/material';
@NgModule({
  imports: [
    MaterialModule.forRoot(),
  ],
})

答案 3 :(得分:0)

导入Http。   访问https://groups.google.com/forum/#!topic/angular-material2/ZEHFELl2-Sw

import {HTTP_PROVIDERS} from '@angular/http';
import {MdIcon, MdIconRegistry} from  '@angular2-material/icon';
@Component({
    template:
      <md-toolbar>
         <md-icon class="demo-toolbar-icon">menu</md-icon>
         <span>Default Toolbar</span>
         <span class="demo-fill-remaining"></span>
         <md-icon>code</md-icon>
    </md-toolbar>`,
    directives:[MdIcon],
    providers: [HTTP_PROVIDERS, MdIconRegistry]
})

答案 4 :(得分:0)

对于使用标准图标,您只需将字体放入html(最简单的解决方案就是使用CDN作为字体): <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

文档中描述了其他方式:http://google.github.io/material-design-icons/#icon-font-for-the-web

您还需要(与任何其他材料包一样)将材料模块放入NgModule配置为导入。