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。

相关内容

热门资讯

揭秘攻略!wepoke系统规律... 一、WPK透视辅助简介了解软件请加微:136704302WPK透视辅助是一款在线扑克游戏平台,玩家可...
玩家亲测!aapoker外挂实... 玩家亲测!aapoker外挂实测,wepoke是真的有挂,扑克教程(最初真的是有挂)1、在wepok...
推荐十款!微扑克大厅都是机器人... 推荐十款!微扑克大厅都是机器人,微扑克ai辅助器苹果版,扑克教程(好像是真的有挂)1、在微扑克ai辅...
总算了解!微扑克大厅都是机器人... 总算了解!微扑克大厅都是机器人,德扑之星ai代打,透牌教程(起初真的是有挂)1、超多福利:超高返利,...
实测揭晓!aapoker下载教... 实测揭晓!aapoker下载教程,德州ai机器人,揭秘教程(从前存在有挂)德州ai机器人辅助器中分为...
分享一款!线上德州有后台控制吗... 分享一款!线上德州有后台控制吗,aapoker辅助工具存在,微扑克教程(最初真的是有挂);人气非常高...
一分钟揭秘!德州之星有没有外挂... 一分钟揭秘!德州之星有没有外挂,wepoke辅助技巧,透明教程(本来真的是有挂),您好,wepoke...
记者发布!gg扑克发牌系统,w... 您好,wepokeai代打这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
揭秘一下!德扑胜率最低的牌,微... 揭秘一下!德扑胜率最低的牌,微扑克游戏辅助器,教你教程(从来真的是有挂)是一款可以让一直输的玩家,快...
揭秘!AA POKER下载软件... 揭秘!AA POKER下载软件,德州ai辅助,AI教程(竟然真的是有挂);小薇(透视辅助)致您一封信...