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、不需要AI权限,帮助你快...
玩家必用!wpk辅助挂多功能透... 玩家必用!wpk辅助挂多功能透视工具,总是是有挂(有挂秘笈)1.wpk 选牌创建新账号,点击进入游戏...
今日头条!德扑之星辅助挂多功能... 今日头条!德扑之星辅助挂多功能透视工具,真是是真的有挂(有挂猫腻)1、德扑之星模拟器是什么优化,德扑...
重大消息!德普之星辅助挂多功能... 重大消息!德普之星辅助挂多功能透视工具,原来存在有挂(有挂头条)1、德普之星免费脚本咨询教程、德普之...
重大消息!wpk辅助挂多功能透... 重大消息!wpk辅助挂多功能透视工具,确实真的是有挂(有挂技术)1、游戏颠覆性的策略玩法,独创攻略技...
解密关于!wepoker辅助挂... 解密关于!wepoker辅助挂多功能透视工具,一贯是真的有挂(有挂规律)1、wepoker辅助器安装...
发现玩家!AAPoKer辅助挂... 发现玩家!AAPoKer辅助挂多功能透视工具,总是是真的有挂(存在有挂)1)AAPoKer辅助插件:...
解密关于!AAPoKer辅助挂... 解密关于!AAPoKer辅助挂多功能透视工具,竟然存在有挂(有挂教程)1、实时AAPoKer透视辅助...
关于!微扑克辅助挂多功能透视工... 关于!微扑克辅助挂多功能透视工具,一贯有挂(有挂详细)1、操作简单,无需微扑克手机版透视脚本,只需要...
揭秘一下!微扑克辅助挂多功能透... 揭秘一下!微扑克辅助挂多功能透视工具,果然是有挂(有挂讲解)1、微扑克模拟器是什么优化,微扑克俱乐部...