我正在使用透光角的图标,我试图添加一些定制的图标,我已经有了svg.该文档非常简短地介绍了如何添加自定义图标,并且没有关于如何在添加后实际使用它们的内容.

以下是我try 过的:

该文件位于单独的Custom-ics.ts文件中:

import { LucideIconData, LucideIcons } from "lucide-angular/icons/types";
import { parseSync } from "svgson";

function svgToLucideIconData(svgString: string): LucideIconData {
    const parsed = parseSync(svgString);
    return parsed.children.map(n => [n.name, n.attributes]);
}

function getLucideIcons(data: LucideIconData): LucideIcons {
    return { data };
}

const icon = getLucideIcons(svgToLucideIconData('someSvg'));

export const myIcons = icon;

我的舱里有这个:

import { icons... } from 'lucide-angular';
import { myIcons } from './custom-icons';
import { LUCIDE_ICONS, LucideIconProvider } from 'lucide-angular';

@NgModule({
    imports: [
        LucideAngularModule.pick({ icons... })
    ],
    exports: [
        LucideAngularModule
    ],
    providers: [
        {
          provide: LUCIDE_ICONS,
          multi: true,
          useValue: new LucideIconProvider(myIcons)
        }
    ]
})
export class SharedModule { }

我甚至不知道是否添加了我的自定义图标,更不用说后来如何使用它了,因为没有关于如何提供图标名称的文档.在过go 的几天里,我一直在浏览答案,但总是走进了死胡同,因此任何帮助都将受到高度赞赏.

推荐答案

我让你的要求起作用了,但有个问题.我们只能有视框大小为0 0 24 24的SVG,除此之外的任何东西都是不可见的(例如,字体(512),谷歌字体等).

之所以会发生这种情况,是因为在这个包中,SVG大小是硬编码的,并且在源代码中没有定制它的选项.

我已经用了iconsvg.xyz最终让它工作,一定要参考下面的工作代码,希望你的问题得到解决!

完整代码

import { Component } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {
  LucideAngularModule,
  File,
  Home,
  Menu,
  UserCheck,
  LUCIDE_ICONS,
  LucideIconProvider,
} from 'lucide-angular';

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import 'zone.js';
const routes: Routes = [];
import { LucideIcons, LucideIconData } from 'lucide-angular/icons/types';
import { parseSync } from 'svgson';

function svgToLucideIconData(svgString: string): LucideIconData {
  const parsed = parseSync(svgString);
  console.log(parsed);
  return parsed.children.map((n: any) => [n.name, n.attributes]);
}

function getLucideIcons(data: LucideIconData): LucideIcons {
  return { Qwerty: data }; // this is the icon name! must be uppercase!
}

const icons = getLucideIcons(
  svgToLucideIconData(
    `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 19V6M5 12l7-7 7 7"/></svg>`
  )
);

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}

@Component({
  selector: 'app-root',
  template: `
    <span-lucide name="qwerty" class="my-icon"></span-lucide>
    <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M240-200h120v-240h240v240h120v-360L480-740 240-560v360Zm-80 80v-480l320-240 320 240v480H520v-240h-80v240H160Zm320-350Z"/></svg>
  `,
})
export class App {
  name = 'Angular';
}

@NgModule({
  declarations: [App],
  imports: [
    BrowserModule,
    AppRoutingModule,
    LucideAngularModule.pick({ File, Home, Menu, UserCheck }),
  ],
  providers: [
    {
      provide: LUCIDE_ICONS,
      multi: true,
      useValue: new LucideIconProvider(icons),
    },
  ],
  bootstrap: [App],
})
export class AppModule {}

platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch((err: any) => console.error(err));

Working stackblitz

Typescript相关问答推荐

返回对象的TypScript构造函数隐式地替换This的值来替换super(.)的任何调用者?

VS代码1.88.0中未出现自动导入建议

如何判断输入是否是TypeScript中的品牌类型?

将props传递给子组件并有条件地呈现它''

如何将泛型对象作为返回类型添加到类型脚本中

类型脚本映射类型:从对象和字符串列表到键值对象

接口重载方法顺序重要吗?

为什么我的查询钩子返回的结果与浏览器的网络选项卡中看到的结果不同?

Typescript 类型守卫一个类泛型?

如何在脚本中输入具有不同数量的泛型类型变量的函数?

避免混淆两种不同的ID类型

try 使Angular依赖注入工作

为什么发送空字段?

在构建Angular 应用程序时,有没有办法通过CLI传递参数?

类型';只读<;部分<;字符串|记录<;字符串、字符串|未定义&>';上不存在TS 2339错误属性.属性&q;x&q;不存在字符串

仅当类型为联合类型时映射属性类型

Angular 16:无法覆盖;baseUrl;在tsconfig.app.json中

TS 排除带点符号的键

在Nestjs中,向模块提供抽象类无法正常工作

使用泛型时记录无法建立索引