如何在Nuxt3中从URL加载动态图像?

huangapple go评论54阅读模式
英文:

How to load a dynamic image from a url in Nuxt3?

问题

I have a nuxt 3 component which fails to load dynamic image that I'm getting from a specific URL (auth0 images).

My template looks like so:

<template>
     <img class="h-28 w-28 rounded-full border-4 border-black bg-gray-300" :src="image" alt=" " />
     <p> {{{name}} {{image}} </p>
</template>

And my script setup is like so:

<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>

The name and image display in the paragraph, but the image URL is not present in the src attribute, and thus the image does not show. The image actually renders after I go back and type something in VSCode. Any idea how I can make the image render?

英文:

I have a nuxt 3 component which fails to load dynamic image that i'm getting from a specific URL(auth0 images).

My template looks like so:

&lt;template&gt;
     &lt;img class=&quot;h-28 w-28 rounded-full border-4 border-black bg-gray-300&quot; :src=&quot;image&quot; alt=&quot; &quot; /&gt;
     &lt;p&gt; {{{name}} {{image}} &lt;/p&gt;
&lt;/template&gt;

And my script setup is like so:

&lt;script setup&gt;
  import { useAuth0 } from &#39;@auth0/auth0-vue&#39;;
  
  let auth0 = process.client ? useAuth0() : undefined;
  let name = ref(&#39;&#39;);
  let image = ref(&#39;&#39;);

  //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;
  }
&lt;/script&gt;

The name and image display in the paragraph, but image url is not present in the src attribute and thus the image does not show. The image actually renders after I go back and type something in VSCode. Any idea how I can make the image render?

答案1

得分: 1

它应该被定义为一个计算属性,因为它依赖于另一个属性的值(对name做同样的事情):

&lt;script setup&gt;

  import { useAuth0 } from &#39;@auth0/auth0-vue&#39;;
  
  let auth0 = process.client ? useAuth0() : undefined;

  const name = computed(()=&gt;auth0?.isAuthenticated ? auth0?.user?.value.nickname:&#39;&#39;);
  const image = computed(()=&gt;auth0?.isAuthenticated ? auth0?.user?.value.picture:&#39;&#39;);

&lt;/script&gt;
英文:

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

&lt;script setup&gt;

  import { useAuth0 } from &#39;@auth0/auth0-vue&#39;;
  
  let auth0 = process.client ? useAuth0() : undefined;

  const name = computed(()=&gt;auth0?.isAuthenticated ? auth0?.user?.value.nickname:&#39;&#39;);
  const image = computed(()=&gt;auth0?.isAuthenticated ? auth0?.user?.value.picture:&#39;&#39;);


&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年4月13日 15:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定