Nuxt 3 图片 src 绑定

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

Nuxt 3 image src binding

问题

我正在尝试使用Nuxt3来渲染头像图片。
到目前为止,我有以下代码:

  1. <template>
  2. ...
  3. <img :src="avatarUrl.value"/>
  4. ...
  5. </template>
  6. <script setup>
  7. const avatarUrl = ref('')
  8. const {data: {user: currentUser}} = await supabase.auth.getUser()
  9. onMounted(async () => {
  10. if (currentUser) {
  11. let {data, error} = await supabase.from('Profiles').select('avatar_url').eq('user_id', currentUser.id).single()
  12. // 输出正确的URL,例如:
  13. // https://lh3.googleusercontent.com/a/[...]fsV4=s96-c
  14. console.log(data.avatar_url)
  15. if (!error) avatarUrl.value = data.avatar_url
  16. }
  17. })
  18. </script>

但是图片不会显示出来。
渲染的HTML是 <img data-v-inspector="components/Header.vue:55:15">,所以它甚至没有src属性。

如何使用从数据库接收到的src动态渲染图片?

英文:

I am trying to render an avatar image using Nuxt3.
So far I have

  1. &lt;template&gt;
  2. ...
  3. &lt;img :src=&quot;avatarUrl.value&quot;/&gt;
  4. ...
  5. &lt;/template&gt;
  6. &lt;script setup&gt;
  7. const avatarUrl = ref(&#39;&#39;)
  8. const {data: {user: currentUser}} = await supabase.auth.getUser()
  9. onMounted(async () =&gt; {
  10. if (currentUser) {
  11. let {data, error} = await supabase.from(&#39;Profiles&#39;).select(&#39;avatar_url&#39;).eq(&#39;user_id&#39;, currentUser.id).single()
  12. // Logs the correct url. eg:
  13. // https://lh3.googleusercontent.com/a/[...]fsV4=s96-c
  14. console.log(data.avatar_url)
  15. if (!error) avatarUrl.value = data.avatar_url
  16. }
  17. })
  18. &lt;/script&gt;

But the image won't display.
The rendered html is &lt;img data-v-inspector=&quot;components/Header.vue:55:15&quot;&gt;, so it doesn't even have a src.

How to dynamically render the img using the src received from the db?

答案1

得分: 1

我找到了解决方法。我必须使用<img :src="avatarUrl"/>

英文:

I've found the fix. I had to use &lt;img :src=&quot;avatarUrl&quot;/&gt;.

huangapple
  • 本文由 发表于 2023年7月13日 00:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672616.html
匿名

发表评论

匿名网友

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

确定