T
T
tyoma_koder2021-10-31 12:35:26
Vue.js
tyoma_koder, 2021-10-31 12:35:26

How to get code of svg file in vue?

I tried to get svg using fetch, the code is displayed in the console, but when I try to insert it into html I get the string "[object Promise]", options like getFileContent(url).then(a=>{return a}) also do not work

async getFileContent(url) {
      const response = await fetch(url, {});
      const json = await response.text();
      console.log(json)
      return json;
 }

I installed the svg loader package, but there are problems with versions (it uses outdated ones as far as I understand)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-10-31
@tyoma_koder

You need to learn what async is, what is Promise, what is async\ await. Without it, you won't go anywhere.
You need to do either this: or this . always returns . You have to wait for the 'a to complete one way or another. If you do so - of course you have lies . This one is just starting execution and there is no data there. Data will probably arrive in it in an hour if you are on a modem. It has nothing to do with Vue. this.svg = await getFileContent(url)
getFileContent(url).then(a => this.svg = a)
PromisePromisePromise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question