我无法使用md-icon显示Google Material中的图标。对于那些不需要太多阐述的人来说,这里是代码,更多细节在代码之后。
app.component(我显示所有主要组件,实际的md-icon位于header.component中):
import { Component } from '@angular/core';
import { HeaderComponent } from './shared/header.component';
import { ContentComponent } from './shared/content.component';
import { FooterComponent } from './shared/footer.component';
@Component({
selector: 'my-app',
template: `<div id="wrapper">
<header></header>
<content></content>
<footer></footer>
<div>`,
directives: [HeaderComponent, ContentComponent, FooterComponent]
})
export class AppComponent { }
标题组件(我尝试调用md-icon):
import { Component } from '@angular/core';
import {MdIcon} from '@angular2-material/icon';
@Component({
selector: 'header',
template: `<md-icon>home</md-icon>
<h1>This is the Header</h1>
`,
directives: [MdIcon]
})
export class HeaderComponent { }
这里是根据google材料网站上的步骤使用他们的图标的css和html:
CSS:
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url("../fonts/MaterialIcons-Regular.eot");
/* For IE6-8 */
src: local("Material Icons"), local("MaterialIcons-Regular"), url("../fonts/MaterialIcons-Regular.woff2") format("woff2"), url("../fonts/MaterialIcons-Regular.woff") format("woff"), url("../fonts/MaterialIcons-Regular.ttf") format("truetype");
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
/* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}
HTML:
<html>
<head>
<title>Boiler Plate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/styles/layout.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err) {
console.error(err);
});
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
最初我没有得到任何错误,只是将md-icon渲染为&#34; home&#34;用纯文本。但是我想我错过了MdIcon的指令,所以我插入了它,现在我收到了这个错误&#34;没有MdIconRegistry的提供者!&#34;。他们在docs中谈论了MdIconRegistry,但从我阅读它的方式来看,如果你不想使用我想要使用的标准谷歌图标,你只需要这样做。所以基本上我导入了组件,包含了指令,在我的索引中添加了链接,并将css代码添加到我的样式表中。从我所看到它应该工作,但我显然遗漏了一些东西。
任何帮助都会受到赞赏,如果有人对与实际问题无关的代码有任何批评,请随时告诉我,我是Angular 2的新手,我可以使用我能得到的所有建议。
提前致谢。
答案 0 :(得分:3)
好的,所以我找到了我所遗漏的东西,它是在文档中,我只是找不到合适的地方。在main.ts文件中,您需要导入和引导HTTP_Providers,在组件内部,您需要导入MdIcon和MdIconRegistry以及包含MdIconRegistry的提供程序。所有这些意味着什么?我很高兴你问过!
Main.ts之前:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
Main.ts之后:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { HTTP_PROVIDERS } from '@angular/http';
bootstrap(AppComponent, [
HTTP_PROVIDERS
]);
之前的header.component.ts:
import { Component } from '@angular/core';
import {MdIcon} from '@angular2-material/icon';
@Component({
selector: 'header',
template: `<md-icon>home</md-icon>`,
directives: [MdIcon]
})
export class HeaderComponent { }
header.component.ts之后:
import { Component } from '@angular/core';
import {MdIcon, MdIconRegistry} from '@angular2-material/icon';
@Component({
selector: 'header',
template: `<md-icon>home</md-icon>`,
directives: [MdIcon],
providers: [MdIconRegistry]
})
export class HeaderComponent { }
答案 1 :(得分:1)
在此正确答案:您必须正确设置项目。您没有以正确的方式导入图标。你必须做什么,但导入图标和字体。
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
您可以在html中导入此内容,而不是其他任何内容。你所做的一切都是一种奇怪的解决方法。
答案 2 :(得分:0)
<!--
For Material Design Icons
The class '.material-icons' is auto-added if a style has NOT been specified
since `material-icons` is the default fontset. So your markup:
-->
<md-icon> face </md-icon>
<!-- becomes this at runtime: -->
<md-icon md-font-set="material-icons"> face </md-icon>
<!-- If the fontset does not support ligature names, then we need to use the ligature unicode.-->
<md-icon> </md-icon>
<!-- The class '.material-icons' must be manually added if other styles are also specified-->
<md-icon class="material-icons md-light md-48"> face </md-icon>