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。

相关内容

热门资讯

最新通报!雀友会潮汕辅助器,微... 最新通报!雀友会潮汕辅助器,微乐小程序微乐家乡辅助器,AI教程(有挂秘诀);大家肯定在之前微乐小程序...
终于知道!蜜瓜大厅辅助下载,新... 终于知道!蜜瓜大厅辅助下载,新蜜瓜大厅破解,技巧教程(有挂解惑);蜜瓜大厅辅助下载简单的灵活控制,仅...
实测分享!微信下程序游戏破解器... 实测分享!微信下程序游戏破解器,凑一桌游戏辅助器,第三方教程(有挂助手);超受欢迎的微信下程序游戏破...
指导大家!西兵辅助器,樱花之盛... 指导大家!西兵辅助器,樱花之盛辅助软件下载,规律教程(竟然有挂);大家肯定在之前樱花之盛辅助软件下载...
必看攻略!多乐辅助app,广东... 必看攻略!多乐辅助app,广东雀神辅助器透视,实用技巧(新版有挂);详细广东雀神辅助器透视攻略(广东...
玩家必看科普!微信卡农辅助,蛮... 玩家必看科普!微信卡农辅助,蛮王大厅辅助插件,2025教程(有挂规律)是一款可以让一直输的玩家,快速...
必知教程!途游四川好运符,微乐... 必知教程!途游四川好运符,微乐福建辅助器,安装教程(有挂技术);途游四川好运符是一种具有地方特色的麻...
一分钟了解!蛮王大厅房卡辅助,... 您好:蛮王大厅房卡辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
我来教教大家!微乐麻将脚本透视... 我来教教大家!微乐麻将脚本透视,闲来辅助软件靠谱,辅助教程(有挂助手);相信小伙伴都知道这个闲来辅助...
一分钟了解!天酷大厅辅助,天酷... 一分钟了解!天酷大厅辅助,天酷游戏交易平台,揭秘教程(今日头条)相信很多朋友都在电脑上玩过天酷游戏交...