[英]Angular Material md-icon error No provider for Http
使用<md-icon>
此錯誤:
原始例外:沒有 Http 提供程序!
所以我在我的組件中添加了HTTP_PROVIDERS
並解決了它。 所以我的問題......為什么我需要將HTTP_PROVIDERS
添加到我的組件中才能讓<md-icon>
工作,即使我沒有在我的應用程序中使用HTTP_PROVIDERS
否則?!
這是我的工作組件。 從 providers 數組中刪除HTTP_PROVIDERS
會引發上述錯誤。
import { Component } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { MdIcon, MdIconRegistry } from '@angular2-material/icon';
@Component({
moduleId: module.id,
selector: 'foo-app',
template: `<md-icon>face</md-icon>`,
directives: [ MdIcon ],
providers: [MdIconRegistry, HTTP_PROVIDERS]
})
export class FooAppComponent {
title = 'Material 2 Foo App';
}
另一個注意事項,這一行將顯示沒有 Http 錯誤且不需要HTTP_PROVIDERS
的圖標:
<i class="material-icons">face</i>
稍微查看 angular2-material 的源代碼,md-icon 取決於 angular2 的Http
,這就是為什么您看到需要 HTTP_PROVIDERS。
繼承人的源鏈接: https : //github.com/angular/material2/blob/master/src/components/icon/icon.ts
在 /src/components/icon/icon.ts 中,該類需要MdIconRegistry
其中MdIcon
具有構造函數:
constructor(
private _element: ElementRef,
private _renderer: Renderer,
private _mdIconRegistry: MdIconRegistry) { }
和MdIconRegistry
需要Http
與MdIconRegistry
具有構造函數:
constructor(private _http: Http) {}
我猜 Http 可能用於從 url 獲取圖標? 因此,如果您深入挖掘源代碼的幾個級別,您可以在其中找到 Http。
角 7
對我來說,此錯誤的解決方案是將MatIconRegistry
添加到我的 APP 模塊上的提供程序:
@NgModule({
declarations: [..],
imports: [
..,MatIconModule,
HttpClientModule,
..
],
providers:[ MatIconRegistry ],
exports: [..]
})
export class SomeModule { }
使用 angular 2.1.0 和 angular material 2.0.0-alpha.10 執行以下操作:
import { MaterialModule } from '@angular/material';
@NgModule({
imports: [
MaterialModule.forRoot(),
]
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.