Displaying an svg in an avatar using nuxt ui
Heyo! I'm trying to display an svg with a nuxt-ui component, however I'm unsure how to exactly use this
The library I'm using, dicebear, give me an svg html, functions to generate a png or jpeg, a data uri, etc
I've tried just piping the svg html into the avatar src but that doesn't work as it expects a url
How should I best display this?
https://ui.nuxt.com/components/avatar#props
3 Replies
Here is how I did it.
I used https://nuxt.com/modules/nuxt-svgo
Then I Added it to my modules in nuxt.config.ts
Now out of the box it rendered my svg all white (I have no idea) but loading it as a component worked, so see the extra bit above.
Then I just used it as per the docs.
and
Oh thanks for that! I'll look into that
I did however end up finding something that worked with dicebear, which was the
toDataUri
and toDataUriSync
methods. They return a uri which is a valid src when passed into an Avatar
component and it displayes it fine
...I don't know too much about this and haven't tested it, but from what I can see you can just base64 encode the svg html (using btoa) and then you have yourself a data uri which can be passed in as the src
You get the idea
However this is only needed if the library you're using doesn't have a .toDataUri() method, however dicebear doesYea nice.