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。

相关内容

热门资讯

透视肯定!aapoker真的假... 透视肯定!aapoker真的假的(透视)透视方法(一贯真的有挂)aapoker真的假的是一种具有地方...
透视辅助!aapoker透视怎... 透视辅助!aapoker透视怎么用(透视)透视脚本下载(确实真的是有挂)1、进入到aapoker透视...
透视玄学!aapoker如何设... 透视玄学!aapoker如何设置胜率(透视)透视脚本(确实是真的有挂);1.aapoker如何设置胜...
透视ai代打!aapoker透... 透视ai代打!aapoker透视脚本(透视)ai插件(一贯是有挂);1、任何aapoker透视脚本a...
透视神器!aapoker怎么开... 透视神器!aapoker怎么开辅助器(透视)发牌逻辑(原来有挂)在进入aapoker怎么开辅助器辅助...
透视模拟器!aapoker透视... 透视模拟器!aapoker透视脚本下载(透视)透视脚本(切实存在有挂)进入游戏-大厅左侧-新手福利-...
透视工具!aapoker辅助软... 透视工具!aapoker辅助软件合法吗(透视)透视脚本入口(总是有挂)1、打开软件启动之后找到中间准...
透视好友房!aapoker怎么... 透视好友房!aapoker怎么提高中牌率(透视)透视插件(本来是真的有挂)1、下载好aapoker怎...
透视代打!aa poker透视... 透视代打!aa poker透视软件(透视)透视脚本(其实真的是有挂)1、操作简单,无需注册,只需要使...
透视辅助!aapoker真的假... 透视辅助!aapoker真的假的(透视)辅助插件工具(竟然真的有挂);1)aapoker真的假的辅助...