[英]Using material icons with md-icon in Angular Material 2
我无法获得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 { }
这是css和html,根据google材料网站上使用其图标的步骤进行:
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呈现为纯文本形式的“ home”。 但是我认为我缺少MdIcon的指令,所以我插入了该指令,现在我收到此错误“没有MdIconRegistry的提供者!”。 他们在文档中谈到了MdIconRegistry,但是从我阅读的方式来看,如果您不想使用我想使用的标准google图标,则只需要它们。 因此,基本上我导入了组件,包括了指令,在索引中添加了链接,并将CSS代码添加到了样式表中。 从我阅读的内容来看,它应该可以工作,但显然我缺少一些东西。
我们将不胜感激,如果有人对与实际问题无关的代码有任何批评,请随时告诉我,我对Angular 2还是陌生的,我可以使用我所能获得的所有建议。
提前致谢。
好的,所以我找到了我所缺少的东西,那是在文档中,而我并没有找到正确的地方。 在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 { }
此处的正确答案:您必须正确设置项目。 您没有以正确的方式导入图标。 除了导入图标和字体,您无需执行任何操作。
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
您将其导入到html中,仅此而已。 您所做的一切都是一个怪异的解决方法。
从文档中 :
<!--
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.