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。

相关内容

热门资讯

第6分钟详情!免费途游四川辅助... 第6分钟详情!免费途游四川辅助器(辅助挂)其实确实有挂(技巧教程开挂辅助插件)免费途游四川辅助器ai...
1分钟详情!情怀打七辅助(辅助... 1分钟详情!情怀打七辅助(辅助挂)一贯有挂(必赢方法开挂辅助下载);无需打开直接搜索薇:136704...
第8分钟辅助!微信小程序白金岛... 第8分钟辅助!微信小程序白金岛修改器视频(辅助挂)果然确实有挂(详细教程!开挂辅助神器)您好:微信小...
八分钟开挂!微友破解器辅助插件... 大家好,今天小编来为大家解答微友破解器辅助插件下载这个问题咨询软件客服可以免费测试直接加微信(136...
3分钟辅助!兴动互娱有辅助吗(... 3分钟辅助!兴动互娱有辅助吗(辅助挂)一贯真的是有挂(AI教程开挂辅助插件) >>您好:软件加薇13...
第5分钟辅助!老友广东潮汕麻雀... 老友广东潮汕麻雀辅助是一款专注玩家量身打造的游戏记牌类型软件,在老友广东潮汕麻雀辅助这款游戏中我们可...
7分钟科普!盛世辅助器(辅助挂... 7分钟科普!盛世辅助器(辅助挂)其实真的是有挂(wepoke教程开挂辅助平台) 了解更多开挂安装加(...
第七分钟开挂!温州茶苑辅助器(... 第七分钟开挂!温州茶苑辅助器(辅助挂)原来真的是有挂(靠谱教程开挂辅助插件)>>您好:软件加薇136...
4分钟辅助!微友助手破解版(辅... 4分钟辅助!微友助手破解版(辅助挂)果然是有挂(微扑克教程开挂辅助插件);亲,微友助手破解版这款游戏...
6分钟科普!新518互游脚本(... 6分钟科普!新518互游脚本(辅助挂)其实是有挂的(透牌教程开挂辅助平台)《详细加薇13670430...