Angular页面渲染或生命周期问题:当重新进入页面时,SVGGraphicsElement.getScreenCTM返回错误的平移值。
创始人
2024-10-30 11:30:33
0

问题描述: 当重新进入Angular页面时,使用SVGGraphicsElement.getScreenCTM方法获取SVG元素的平移值时,返回错误的值。

解决方法:

  1. 在Angular组件中,使用ngAfterViewInit生命周期钩子来处理SVG元素的渲染和获取平移值的逻辑。
import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-svg-component',
  template: `
    
      
    
  `,
})
export class SvgComponent implements AfterViewInit {
  @ViewChild('svgElement') svgElementRef: ElementRef;

  ngAfterViewInit(): void {
    this.getSVGElementTranslation();
  }

  getSVGElementTranslation(): void {
    const svgElement = this.svgElementRef.nativeElement;
    const gElement = svgElement.querySelector('g'); // Assuming the SVG element is inside a  element

    if (gElement) {
      const ctm = gElement.getScreenCTM();
      const translationX = ctm.e;
      const translationY = ctm.f;

      console.log('Translation X:', translationX);
      console.log('Translation Y:', translationY);
    }
  }
}
  1. 使用ngAfterViewChecked生命周期钩子来处理SVG元素的渲染和获取平移值的逻辑。
import { Component, AfterViewChecked, ViewChild, ElementRef } from '@angular/core';

@Component({
  selector: 'app-svg-component',
  template: `
    
      
    
  `,
})
export class SvgComponent implements AfterViewChecked {
  @ViewChild('svgElement') svgElementRef: ElementRef;

  ngAfterViewChecked(): void {
    this.getSVGElementTranslation();
  }

  getSVGElementTranslation(): void {
    const svgElement = this.svgElementRef.nativeElement;
    const gElement = svgElement.querySelector('g'); // Assuming the SVG element is inside a  element

    if (gElement) {
      const ctm = gElement.getScreenCTM();
      const translationX = ctm.e;
      const translationY = ctm.f;

      console.log('Translation X:', translationX);
      console.log('Translation Y:', translationY);
    }
  }
}

这两种方法都会在Angular组件初始化完成后,或在Angular渲染周期内检查并获取SVG元素的平移值。根据需要选择适合的生命周期钩子函数。

相关内容

热门资讯

实操分享!hardrock透视... 实操分享!hardrock透视工具(辅助挂)都是是真的有挂(的确有挂辅助攻略)1、透视辅助软件激活码...
揭秘几款!wepoker免费脚... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
分享开挂内幕!哈糖大菠萝有挂吗... 分享开挂内幕!哈糖大菠萝有挂吗5个常用方法(辅助挂)本来是真的有挂(有挂分享辅助技巧)1、实时透视辅...
2分钟细说!wepokerpl... 2分钟细说!wepokerplus透视脚本免费(辅助挂)真是真的有挂(有挂总结辅助软件)1、实时透视...
2024教程!hhpoker智... 2024教程!hhpoker智能辅助插件(辅助挂)真是存在有挂(有挂头条辅助app);1、起透看视 ...
玩家必备教程!wpk透视插件(... 玩家必备教程!wpk透视插件(辅助挂)本来真的是有挂(有挂教学辅助教程)1、点击下载安装,脚本分类一...
推荐十款!wepoker透视脚... 推荐十款!wepoker透视脚本免费下载pc(辅助挂)都是有挂(有挂方法辅助方法)1、下载好脚本下载...
玩家亲测!hhpoker买挂(... 玩家亲测!hhpoker买挂(辅助挂)果然有挂(有挂方针辅助工具)能透视中分为三种模型:模拟器、私人...
一分钟教你!aapoker透视... 一分钟教你!aapoker透视方法(辅助挂)本来是真的有挂(的确有挂辅助app)亲,关键说明,透视脚...
我来教教大家!wepoker作... 我来教教大家!wepoker作弊方法(辅助挂)确实有挂(有挂秘诀辅助教程)1、该软件可以轻松地帮助玩...