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。

相关内容

热门资讯

科技通报!途游四川破解,epo... 科技通报!途游四川破解,epoker底牌透视,2025新版教程(有挂透视);途游四川破解软件透明挂作...
9分钟了解!欢乐情怀开挂,we... 您好:欢乐情怀开挂这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
必知教程!超凡辅助平台,悟空黑... 必知教程!超凡辅助平台,悟空黑桃a有挂嘛,透视教程(有挂头条);大家肯定在之前超凡辅助平台或者超凡辅...
一分钟秒懂!蛮王大厅辅助,德州... 您好,蛮王大厅辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款...
重大通报!宁夏划水辅助,hhp... 重大通报!宁夏划水辅助,hhpoker为什么一直输,2025教程(有挂辅助)是一款可以让一直输的玩家...
分享个大家!都莱软件安装辅助,... 分享个大家!都莱软件安装辅助,wejoker黑侠辅助器,科技教程(有挂技术);1、很好的都莱软件安装...
来一盘!辅助软件购买平台,We... 您好:辅助软件购买平台这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
重大科普!创思维激k辅助神器,... 重大科普!创思维激k辅助神器,hhpoker作弊码,揭秘教程(有挂教学);大家肯定在之前创思维激k辅...
交流学习经验!朋友局app辅助... 交流学习经验!朋友局app辅助器下载,aapoker辅助怎么用,揭秘教程(有挂解密);小薇(透视辅助...
技术分享!开心泉州小程序辅助器... 技术分享!开心泉州小程序辅助器,wpk辅助哪里买,分享教程(真是有挂)是一款可以让一直输的玩家,快速...