Angular再次加载预加载的svg。
创始人
2024-10-30 19:30:21
0

要在Angular中重新加载预加载的SVG,你可以使用Angular的动态组件加载功能来实现。下面是一个示例解决方案的代码:

  1. 在你的组件中定义一个变量,用于存储SVG的URL:
svgUrl: string = 'assets/icons/my-icon.svg';
  1. 在模板中使用img标签来加载SVG:
My Icon
  1. 创建一个动态组件加载器服务SvgLoaderService,用于重新加载SVG:
import { Injectable, ComponentFactoryResolver, ViewContainerRef, ComponentRef, Type } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SvgLoaderService {
  private componentRef: ComponentRef;

  constructor(private resolver: ComponentFactoryResolver) { }

  loadSvg(svgUrl: string, container: ViewContainerRef, componentType: Type): Promise {
    return new Promise((resolve, reject) => {
      const factory = this.resolver.resolveComponentFactory(componentType);
      container.clear();
      const componentRef = container.createComponent(factory);

      const img = new Image();
      img.onload = () => {
        componentRef.instance.svgUrl = svgUrl;
        componentRef.changeDetectorRef.detectChanges();
        this.componentRef = componentRef;
        resolve();
      };
      img.onerror = () => {
        reject();
      };
      img.src = svgUrl;
    });
  }

  reloadSvg(svgUrl: string): Promise {
    return new Promise((resolve, reject) => {
      if (this.componentRef) {
        const img = new Image();
        img.onload = () => {
          this.componentRef.instance.svgUrl = svgUrl;
          this.componentRef.changeDetectorRef.detectChanges();
          resolve();
        };
        img.onerror = () => {
          reject();
        };
        img.src = svgUrl;
      } else {
        reject();
      }
    });
  }
}
  1. 在你的组件中注入SvgLoaderService并使用它来加载和重新加载SVG:
import { Component, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
import { SvgLoaderService } from './svg-loader.service';

@Component({
  selector: 'app-svg-component',
  template: `
    
  `,
})
export class SvgComponent implements OnInit {
  @ViewChild('svgContainer', { read: ViewContainerRef }) svgContainer: ViewContainerRef;

  constructor(private svgLoaderService: SvgLoaderService) { }

  ngOnInit() {
    this.loadSvg();
  }

  loadSvg() {
    this.svgLoaderService.loadSvg(this.svgUrl, this.svgContainer, MySvgComponent)
      .then(() => {
        console.log('SVG loaded successfully!');
      })
      .catch(() => {
        console.error('Failed to load SVG.');
      });
  }

  reloadSvg() {
    this.svgLoaderService.reloadSvg(this.svgUrl)
      .then(() => {
        console.log('SVG reloaded successfully!');
      })
      .catch(() => {
        console.error('Failed to reload SVG.');
      });
  }
}
  1. 创建一个用于显示SVG的动态组件MySvgComponent
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-my-svg',
  template: `
    My Icon
  `,
})
export class MySvgComponent {
  @Input() svgUrl: string;
}

现在,你可以使用SvgComponent来加载和重新加载预加载的SVG。调用loadSvg()方法来加载SVG,调用reloadSvg()方法来重新加载SVG。

相关内容

热门资讯

第五分钟开挂!随意玩聚乐部辅助... 第五分钟开挂!随意玩聚乐部辅助,中至江西插件,揭秘攻略-2026最新版本1.随意玩聚乐部辅助 ai辅...
据通报!广东闲来辅助免费(辅助... 您好:广东闲来辅助免费这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
1分钟开挂!聚友联盟免费辅助器... 1分钟开挂!聚友联盟免费辅助器,老夫子拼十辅助,必备教程-2026最新版本1、聚友联盟免费辅助器ai...
现有说明如下!闲逸碰胡辅助软件... 现有说明如下!闲逸碰胡辅助软件(辅助挂)其实有挂(有挂举措)-哔哩哔哩;无需打开直接搜索加(薇:13...
2分钟开挂!微乐小程序辅助器出... 2分钟开挂!微乐小程序辅助器出租,佛手在线大菠萝辅助,揭秘教程-2026最新版本1、完成微乐小程序辅...
目前来看!金虎爷辅助(辅助挂)... 您好:金虎爷辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好...
7分钟开挂!乐乐川南字牌辅助器... 7分钟开挂!乐乐川南字牌辅助器,川友汇辅助软件,切实教程-2026最新版本1、玩家可以在乐乐川南字牌...
为切实保障!情怀国粹麻将神器(... 为切实保障!情怀国粹麻将神器(辅助挂)原来真的是有挂(有挂模板)-哔哩哔哩;1、完成情怀国粹麻将神器...
第5分钟开挂!宝宝游戏辅助器,... 第5分钟开挂!宝宝游戏辅助器,科技新星游牛牛辅助,安装教程-2026最新版本1、全新机制【科技新星游...
经调查!一键装方片十三张辅助(... 经调查!一键装方片十三张辅助(辅助挂)果然确实有挂(有挂法门)-哔哩哔哩>>您好:软件加薇13670...