Nuxt 3 图片 src 绑定

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

Nuxt 3 image src binding

问题

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

    <template>
       ...
       <img :src="avatarUrl.value"/>
       ...
    </template>

    <script setup>
    const avatarUrl = ref('')

    const {data: {user: currentUser}} = await supabase.auth.getUser()

    onMounted(async () => {
      if (currentUser) {
        let {data, error} = await supabase.from('Profiles').select('avatar_url').eq('user_id', currentUser.id).single()

        // 输出正确的URL,例如:
        // https://lh3.googleusercontent.com/a/[...]fsV4=s96-c
        console.log(data.avatar_url)

        if (!error) avatarUrl.value = data.avatar_url
      }
    })
    </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

    &lt;template&gt;
       ...
       &lt;img :src=&quot;avatarUrl.value&quot;/&gt;
       ...
    &lt;/template&gt;

    &lt;script setup&gt;
    const avatarUrl = ref(&#39;&#39;)

    const {data: {user: currentUser}} = await supabase.auth.getUser()

    onMounted(async () =&gt; {
      if (currentUser) {
        let {data, error} = await supabase.from(&#39;Profiles&#39;).select(&#39;avatar_url&#39;).eq(&#39;user_id&#39;, currentUser.id).single()

        // Logs the correct url. eg: 
        // https://lh3.googleusercontent.com/a/[...]fsV4=s96-c
        console.log(data.avatar_url)

        if (!error) avatarUrl.value = data.avatar_url
      }
    })
    &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:

确定