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。

相关内容

热门资讯

透视计算!德普辅助器怎么用,德... 透视计算!德普辅助器怎么用,德普辅助器怎么用(透视)一直有挂(有挂分享)-哔哩哔哩德普辅助器怎么用辅...
一直以来!威信茶馆大贰破解,新... 一直以来!威信茶馆大贰破解,新上游辅助器,策略教程(有挂技巧)-哔哩哔哩1.威信茶馆大贰破解 选牌创...
出乎意料的是!雀姬辅助脚本,微... 出乎意料的是!雀姬辅助脚本,微乐春天小程序辅助,模块教程(有挂攻略)-哔哩哔哩1、用户打开应用后不用...
透视辅助!德普辅助软件,德扑圈... 透视辅助!德普辅助软件,德扑圈透视挂(透视)确实存在有挂(新版有挂)-哔哩哔哩1、完成德扑圈透视挂辅...
透视ai代打!德普之星透视辅助... 透视ai代打!德普之星透视辅助软件是真的吗,德普之星的辅助工具介绍(透视)真是是有挂(有挂神器)-哔...
现就发布提示!闲逸辅助软件,w... 现就发布提示!闲逸辅助软件,wepoker插件下载,攻略教程(有挂讲解)-哔哩哔哩小薇(辅助器软件下...
透视黑科技!德普之星私人局辅助... 透视黑科技!德普之星私人局辅助免费,德普辅助器怎么用(透视)果然是真的挂(有挂透视)-哔哩哔哩1、玩...
目前来看!土豪联盟辅助器,wp... 目前来看!土豪联盟辅助器,wpk俱乐部辅助器,资料教程(有挂存在)-哔哩哔哩1、wpk俱乐部辅助器模...
透视私人局!德普辅助软件,德扑... 透视私人局!德普辅助软件,德扑圈有透视吗(透视)好像真的有挂(有挂方略)-哔哩哔哩所有人都在同一条线...
出乎意料的是!粤麻圈辅助器,微... 出乎意料的是!粤麻圈辅助器,微乐自建房怎么提高胜率,项目教程(有挂透视)-哔哩哔哩1、完成微乐自建房...