Home  >  Q&A  >  body text

How to load dynamic images from url in Nuxt3?

<p>I have a nuxt 3 component that cannot load a dynamic image that I get from a specific URL (auth0 image). </p> <p>My template looks like this:</p> <pre class="brush:php;toolbar:false;"><template> <img class="h-28 w-28 rounded-full border-4 border-black bg-gray-300" :src="image" alt=" " /> <p> {{{name}} {{image}} </p> </template></pre> <p>My script is set up as follows:</p> <pre class="brush:php;toolbar:false;"><script setup> import { useAuth0 } from '@auth0/auth0-vue'; let auth0 = process.client ? useAuth0() : undefined; let name = ref(''); let image = ref(''); //check user authentication state and set name and image if(auth0?.isAuthenticated){ name.value = auth0?.user.value.nickname; //image url: https://.... image.value = auth0?.user?.value.picture; } </script></pre> <p>The name and image appear in the paragraph, but the image url does not exist in the src attribute, so the image does not appear. When I go back and type something in VSCode, the image actually renders. Any idea how to render an image? </p>
P粉236743689P粉236743689416 days ago526

reply all(1)I'll reply

  • P粉704066087

    P粉7040660872023-08-31 00:57:19

    It should be defined as a computed property because it depends on the value of another property (do the same for name):

    <script setup>
    
      import { useAuth0 } from '@auth0/auth0-vue';
      
      let auth0 = process.client ? useAuth0() : undefined;
    
      const name = computed(()=>auth0?.isAuthenticated ? auth0?.user?.value.nickname:'');
      const image = computed(()=>auth0?.isAuthenticated ? auth0?.user?.value.picture:'');
    
    
    </script>
    

    reply
    0
  • Cancelreply