A lightweight library that makes it easier to use SVG icons in your Angular Application
The svg-icon
library enables using the <svg-icon>
tag to directly display SVG icons in the DOM.This approach offers an advantage over using an <img>
tag or via the CSS background-image
property, because it allows styling and animating the SVG with CSS.
For example, if the fill
or stroke
properties of elements in the svg are set to currentColor
, they will have the color defined for the containing DOM element. So the color can easily be changed by changing the color style on the svg-icon
element.
ng add @ngneat/svg-icon
src/assets/svg
tsconfig
file:{
...
"paths": {
"@app/svg/*": ["src/app/svg/*"]
}
},
@ngneat/svg-generator
to clean and extract the icons content:{
"scripts": {
"generate-icons": "svgGenerator"
},
"svgGenerator": {
"outputPath": "./src/app/svg",
"prefix": "app",
"srcPath": "./src/assets/svg",
"svgoConfig": {
"plugins": [
"removeDimensions"
]
}
}
}
npm run generate-icons
Import the SvgIconsModule
in your AppModule
, and register the icons:
import { SvgIconsModule } from '@ngneat/svg-icon';
import { settingsIcon } from '@app/svg/settings';
@NgModule({
imports: [
SvgIconsModule.forRoot({
icons: [settingsIcon],
}),
],
})
export class AppModule {}
Now we can use the svg-icon
component:
<svg-icon key="settings"></svg-icon> <svg-icon key="settings" color="hotpink" fontSize="40px"></svg-icon>
In lazy load modules or in reusable component modules, we can use the forChild
method, for register icons accessible locally in these modules:
import { dashboardIcon } from '@app/svg/dashboard';
import { userIcon } from '@app/svg/user';
import { SvgIconsModule } from '@ngneat/svg-icon';
@NgModule({
declarations: [DashboardComponent],
imports: [DashboardRoutingModule, SvgIconsModule.forChild([userIcon])],
})
export class DashboardModule {}
Note that we're NOT using a barrel file (i.e index.ts
). This will make sure we only load the SVG files we use in the current module.
To make the process more seamless, the library provides a Webpack plugin you can use to automate the extracting process:
const { SvgGeneratorWebpackPlugin } = require('@ngneat/svg-generator/webpack-plugin');
{
plugins: [
new SvgGeneratorWebpackPlugin({
watch: !isProd,
srcPath: './src/assets/svg',
outputPath: './src/app/svg',
svgoConfig: {
plugins: [
"removeDimensions"
],
},
}),
];
}
There are cases where we want to group multiple SVG icons. For example, we might have a notifications
feature, and we need to load SVG icons such as Slack, Email, etc.
In such cases, create a unique directory, and put the related icons inside it. For example:
home.svg
user.svg
/notifications
- slack.svg
- email.svg
This will create a notifications
folder with a barrel
file that export the SVG icons inside the folder under a const named ${folderName}Icons
:
import { notificationsIcons } from '@app/svg/notifications';
@NgModule({
imports: [SvgIconsModule.forChild(notificationsIcons)],
})
export class NotificationsModule {}
To control the SVG size, we use the font-size
property as described in this article.You also have the option to pass fixed sizes and use them across the application:
@NgModule({
imports: [
SvgIconsModule.forRoot({
sizes: {
xs: '10px',
sm: '12px',
md: '16px',
lg: '20px',
xl: '25px',
xxl: '30px'
},
defaultSize: 'md'
icons
})
],
bootstrap: [AppComponent]
})
export class AppModule {}
They are used in the size
input:
<svg-icon key="settings" size="lg"></svg-icon>
@Input() key: string;
@Input() size: string;
@Input() fontSize: string;
@Input() color: string;
@Input() width: string | number;
@Input() height: string | number;
You can inject the SvgIconRegistry
, and get existing SVG icons or register new ones:
import { SvgIconRegistry } from '@ngneat/svg-icon';
interface Icon {
name: string;
data: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
constructor(private registry: SvgIconRegistry) {
registry.register([Icon, Icon, Icon]);
registry.register(Icon);
registry.get(name);
registry.getAll();
}
}
You can define missingIconFallback which will be used if icon is not found in registry:
import { unknownIcon } from '@app/svg/unknown';
@NgModule({
imports: [
SvgIconsModule.forRoot({
missingIconFallback: unknownIcon,
}),
],
bootstrap: [AppComponent],
})
export class AppModule {}
Thanks goes to these wonderful people (emoji key):
![]() Netanel Basal |
![]() Inbal Sinai |
![]() Shahar Kazaz |
![]() Artur Androsovych |
![]() Vlad Tansky |
![]() Zura Gabievi |
This project follows the all-contributors specification. Contributions of any kind welcome!
Logo icon was made by Freepik from www.flaticon.com
准备 安装 svg-sprite-loader npm i svg-sprite-loader@6.0.11 下载 iconfont 并导入项目 在 assets 目录下新建 icons 目录,在 icons 目录下继续新建 svg 目录(用来存放svg图标)。 在 svg 目录下新建 user.svg 文件。 从 iconfont-阿里巴巴矢量图标库官方搜索 user,选择需要的图标,复制 S
安装svg插件 npm i svg-sprite-loader@5.2.1 创建svg组件 在src 下的 components 文件夹中创建SvgIcon文件夹,在SvgIcon文件夹下创建index.vue文件 index.vue的代码如下 <template> <svg :class="svgClass" aria-hidden="true" v-on="$lis
vue+webpack中svg-icon使用 在webpack.base.conf.js中配置svg-icon // svg-icon配置 { test: /\.svg$/, loader: 'svg-sprite-loader', include: [resolve('src/icons')], options: {
支付网关图标 支付网关图标对于电子商务应用服务提供商是有用的。 下面提供了一些授权电子商务,在线零售商,砖块和点击或传统砖公司图标的信用卡付款。
描述 (Description) svg-gradient是一种颜色到另一种颜色的过渡。 它可以为同一个元素添加许多颜色。 它至少包含三个参数 - 第一个参数标识渐变类型和方向。 其他参数列出其位置和颜色。 在第一个和最后一个位置指定的颜色是可选的。 可以设置方向 - 从中心到底部,右边,右下角,右上角,椭圆或椭圆。 参数 - 颜色在列表中停止 - list - 列出所有颜色及其位置。 esc
关于 SVG GIF、 JPEG、 WBMP 和 PNG 等用于 Web 的位图图像格式,都使用像素网格来描述图像。生成的文件有可能很庞大,局限于单一(通常较低)的分辨率,且在 Web 上会占用大量带宽。 SVG 是将图像描述为形状、路径、文本和滤镜效果的矢量格式。生成的文件很小,可在 Web、打印甚至资源有限的手持设备上提供较高品质的图像。用户无须牺牲锐利程度、细节或清晰度,即可在屏幕上放大 S
运动路径动画 动画一个元素,使之相对于SVG路径的x,y和角度值运动。 var myPath = anime.path('svg path'); path函数返回一个返回指定属性的新函数。 v3版本以后,运动路径动画具有响应性(responsive) Parameters Example Info 'x' myPath('x') 返回SVG路径的当前x值('px') 'y' myPath('y')
SVG代表S calable V ector G raphics,它是一种用XML描述2D图形和图形应用程序的语言,然后由SVG查看器呈现XML。 SVG主要用于矢量类型图,如饼图,X,Y坐标系中的二维图等。 SVG成为W3C推荐标准14. 2003年1月,您可以在SVG规范中查看最新版本的SVG规范 。 查看SVG文件 大多数Web浏览器都可以显示SVG,就像它们可以显示PNG,GIF和JPG一
SVG 工具 优化: SVGOMG Peter Collingridge's SVG Optimiser SVGO SVGO-GUI SVG Cleaner Scour SVG Scrubber Clean Multiple Gradient Stops SVG 编辑器: Illustrator Sketch Inkscape DrawSVG 创建雪碧图: Icomoon Fontastic Gr