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。

相关内容

热门资讯

盘点透视"wpk软件... 盘点透视"wpk软件辅助挂"开挂(透视)辅助平台(科技揭秘可靠技巧)盘点透视"wpk软件辅助挂"开挂...
实测辅助!金虎爷辅助器,微乐小... 微乐小程序辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
教会透视"微扑克辅助... 教会透视"微扑克辅助器官网入口"开挂(透视)辅助工具(一分钟揭秘AI教程)教会透视"微扑克辅助器官网...
详细开挂!樱花之盛牛牛挂,来来... 您好:来来拼十辅助免费下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
教会透视"有wepo... 教会透视"有wepoker透视吗"开挂(透视)辅助工具(我来教大家科技教程)教会透视"有wepoke...
推荐开挂!随意玩聚乐部辅助,雀... 【亲,雀友会钻石辅助器潮汕麻将 这款游戏可以开挂的,确实是有挂的,很多玩家在这款雀友会钻石辅助器潮汕...
推荐透视"wepok... 推荐透视"wepoker透视辅助挂的游戏特色"开挂(透视)辅助神器(最新研发专业教程)推荐透视"we...
了解辅助!新漫游大厅辅助器,枫... 您好:枫叶辅助官网下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
推荐透视"wepok... 推荐透视"wepoker透视挂方法"开挂(透视)辅助平台(必看攻略插件教程)推荐透视"wepoker...
详细辅助!椰子大厅辅助脚本,5... 详细辅助!椰子大厅辅助脚本,518互游辅助(玩家必看科普开挂辅助安装);小薇(透视辅助)致您一封信;...