Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

422
Views
Cómo convertir SVG a cadena Base64 en Angular

quiero convertir un elemento svg en una cadena base64.

Seguí el siguiente tutorial: https://thewebdev.info/2021/08/28/how-to-convert-inline-svg-to-base64-string-with-javascript/

este es el código que usé:

HTML:

 <svg height="100" width="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg>

Mecanografiado:

 constructor ( @Inject(DOCUMENT) document: Document ) { document.querySelector("svg") } ngOnInit() { const s = new XMLSerializer().serializeToString(document.querySelector("svg")) const encodedData = window.btoa(s) console.log(encodedData) }

pero recibo el siguiente mensaje de error en el código vs en .serializeToString(document.querySelector("svg")) :

Argumento de tipo 'SVGSVGElement | null' no se puede asignar a un parámetro de tipo 'Node' TS 2345

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema aquí es que svg no se inicializa cuando se llama a ngOnInit, por lo que querySelector devuelve nulo.

Mueva su código en ngAfterViewInit como a continuación: -.

 ngAfterViewInit() { const svg = document.querySelector("svg"); if (svg) { const s = new XMLSerializer().serializeToString(svg) const encodedData = window.btoa(s) console.log(encodedData) } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!