Angular svg不同实例共享相同的defs
创始人
2024-10-20 16:30:50
0

要实现Angular中不同实例共享相同的defs,可以使用Angular的服务(service)来实现。服务是一种可用于在多个组件之间共享数据和功能的机制。

下面是一个示例代码:

  1. 创建一个SVGService服务,用于管理svg的defs元素。
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SVGService {
  private defs: string = '';

  constructor() { }

  setDefs(defs: string) {
    this.defs = defs;
  }

  getDefs() {
    return this.defs;
  }
}
  1. 在组件中使用SVGService服务来设置和获取defs。
import { Component, OnInit } from '@angular/core';
import { SVGService } from './svg.service';

@Component({
  selector: 'app-svg-component',
  template: `
    
      
        
          
        
      
      ...
    
  `,
})
export class SVGComponent implements OnInit {
  defs: string = '';

  constructor(private svgService: SVGService) { }

  ngOnInit() {
    // 获取defs
    this.defs = this.svgService.getDefs();
  }
}
  1. 在另一个组件中设置defs。
import { Component, OnInit } from '@angular/core';
import { SVGService } from './svg.service';

@Component({
  selector: 'app-another-component',
  template: `
    
  `,
})
export class AnotherComponent implements OnInit {

  constructor(private svgService: SVGService) { }

  ngOnInit() { }

  setDefs() {
    const defs = '';
    this.svgService.setDefs(defs);
  }
}
  1. 在根模块中提供SVGService服务。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { SVGService } from './svg.service';
import { SVGComponent } from './svg.component';
import { AnotherComponent } from './another.component';

@NgModule({
  imports: [BrowserModule],
  declarations: [SVGComponent, AnotherComponent],
  providers: [SVGService],
  bootstrap: [SVGComponent, AnotherComponent]
})
export class AppModule { }

这样,不同的SVGComponent实例就可以共享相同的defs了。当点击AnotherComponent中的按钮时,defs将被更新并传递给SVGComponent。

相关内容

热门资讯

随着!好玩贰柒拾辅助"... 随着!好玩贰柒拾辅助"科普辅助器"果然真的是有挂(哔哩哔哩)1、任何好玩贰柒拾辅助透视是真的假的的玩...
出乎意料的是!川娱竞技血战辅助... 出乎意料的是!川娱竞技血战辅助器"揭露辅助教程"本来真的有挂(哔哩哔哩)1、超多福利:超高返利,海量...
2026版复盘!花花生活圈辅助... 2026版复盘!花花生活圈辅助工具安装教程"开挂辅助器"一直是有挂(哔哩哔哩)小薇(辅助器软件下载)...
攻略辅助挂!中至吉安小程序微信... 攻略辅助挂!中至吉安小程序微信小程序"透视辅助脚本"原来真的是有挂(哔哩哔哩)1.中至吉安小程序微信...
透视安卓版!天天爱消除辅助脚本... 透视安卓版!天天爱消除辅助脚本"解谜辅助脚本"切实是真的有挂(哔哩哔哩)1、让任何用户在无需天天爱消...
更值得关注的是!中至九江麻将辅... 更值得关注的是!中至九江麻将辅助器"揭幕辅助攻略"都是是有挂(哔哩哔哩)1、玩家可以在中至九江麻将辅...
透视了解!打两圈辅助"... 透视了解!打两圈辅助"解迷辅助插件"都是真的有挂(哔哩哔哩)小薇(辅助器软件下载)致您一封信;亲爱打...
黑科技技巧!有没有哈糖大菠萝攻... 黑科技技巧!有没有哈糖大菠萝攻略推荐"解谜辅助app"好像存在有挂(哔哩哔哩)1、下载好有没有哈糖大...
透视有挂!决战卡五星辅助&qu... 透视有挂!决战卡五星辅助"解迷辅助器"一直确实有挂(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会...
最终!上饶中至能操控吗&quo... 最终!上饶中至能操控吗"分享辅助方法"一贯存在有挂(哔哩哔哩)上饶中至能操控吗透视方法中分为三种模型...