简体   繁体   English

Angular Material md-icon 错误没有 Http 提供程序

[英]Angular Material md-icon error No provider for Http

When using <md-icon> I get this error:使用<md-icon>此错误:

ORIGINAL EXCEPTION: No provider for Http!原始例外:没有 Http 提供程序!

So I added HTTP_PROVIDERS to my component and it solved it.所以我在我的组件中添加了HTTP_PROVIDERS并解决了它。 So my question... Why do I need to add HTTP_PROVIDERS to my component to get <md-icon> to work even though I'm not using HTTP_PROVIDERS in my app otherwise?!所以我的问题......为什么我需要将HTTP_PROVIDERS添加到我的组件中才能让<md-icon>工作,即使我没有在我的应用程序中使用HTTP_PROVIDERS否则?!

Here's my working component.这是我的工作组件。 Removing HTTP_PROVIDERS from the providers array throws the above error.从 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';
}

One other note, this line will display the icon with no Http error and no need for HTTP_PROVIDERS :另一个注意事项,这一行将显示没有 Http 错误且不需要HTTP_PROVIDERS的图标:

<i class="material-icons">face</i>

Looking into the source code a bit for angular2-material, md-icon depends on angular2's Http which is why you're seeing the need for HTTP_PROVIDERS.稍微查看 angular2-material 的源代码,md-icon 取决于 angular2 的Http ,这就是为什么您看到需要 HTTP_PROVIDERS。

heres a link to the source: https://github.com/angular/material2/blob/master/src/components/icon/icon.ts继承人的源链接: https : //github.com/angular/material2/blob/master/src/components/icon/icon.ts

in /src/components/icon/icon.ts, the class requires MdIconRegistry with MdIcon having the constructor:在 /src/components/icon/icon.ts 中,该类需要MdIconRegistry其中MdIcon具有构造函数:

constructor(
  private _element: ElementRef,
  private _renderer: Renderer,
  private _mdIconRegistry: MdIconRegistry) { }

and MdIconRegistry requires Http with MdIconRegistry having the constructor:MdIconRegistry需要HttpMdIconRegistry具有构造函数:

  constructor(private _http: Http) {}

I guess Http is used to perhaps get icons from a url?我猜 Http 可能用于从 url 获取图标? So if you dig a few levels down into the source code, you can find Http in there.因此,如果您深入挖掘源代码的几个级别,您可以在其中找到 Http。

Angular 7角 7

For me the solution to this error was to add MatIconRegistry to providers on my APP module:我来说,此错误的解决方案是将MatIconRegistry添加到我的 APP 模块上的提供程序:

@NgModule({
  declarations: [..],
  imports: [
   ..,MatIconModule,
    HttpClientModule,
   ..
  ],
  providers:[  MatIconRegistry ],
  exports: [..]
})
export class SomeModule { }

Looking at the source reveals that MdIcon uses MdIconRegistry which uses Http to load the icon. 查看源代码显示 MdIcon 使用 MdIconRegistry 使用 Http 加载图标。 This is also mentioned in a comment for MdIcon. MdIcon评论中也提到了这一点。

It still seems strange that that library is not self contained, but for now that seems to be the way it is.那个库不是自包含的,这似乎仍然很奇怪,但现在似乎就是这样。

Using angular 2.1.0 and angular material 2.0.0-alpha.10 do the following:使用 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.

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