LoginSignup
0
4

More than 3 years have passed since last update.

APIで取ってきた画像をVueでbackground-imageを指定する

Posted at

Vue.jsでbackground-imageを指定する

APIで取得してきたものをhtml内でなんとか指定したいっていう感じ

諦めてimgタグでレイアウト整えようとしたところで解決したので、感動した。

vueなどで普段から書いているフロントエンドエンジニアたちにとってこの書き方ができるかできないかで生涯年収が2倍違うと言っても過言。

index.vue
<template>
  <div :style="{backgroundImage: 'url(' + img[img.length-1].img_url + ')'}">
    HelloWorld!
  </div>
</template>

<script>
export default {
  async asyncData() {
    return await axios
      .get('/api/img/get_img')
      .then(res => {
        return {
         img: res.data.details.img
        }
      })
      .catch(error => {
        // throw error;
      });
  },
}
</script>
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4