Skip to content

Instantly share code, notes, and snippets.

@Atinux
Created July 4, 2017 10:25
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atinux/03e0d471ee7c6439209832ff94aa3017 to your computer and use it in GitHub Desktop.
Save Atinux/03e0d471ee7c6439209832ff94aa3017 to your computer and use it in GitHub Desktop.
Dynamic image nuxt
<template>
<div>
<h1>Welcome!</h1>
<nuxt-link to="/about">About page</nuxt-link>
<img :src="flagUrl"/>
<button @click="nextFlag">Next flag</button>
</div>
</template>
<script>
var images = require.context('~assets/', false, /\.png$/)
export default {
data: () => ({
flags: ['france', 'germany', 'united-states'],
flagIndex: 0
}),
computed: {
flag() {
return this.flags[this.flagIndex]
},
flagUrl() {
return images(`./${this.flag}.png`)
}
},
methods: {
nextFlag() {
if ((this.flagIndex + 1) === this.flags.length) {
this.flagIndex = 0
} else {
this.flagIndex++
}
}
}
}
</script>
@adrianoresende
Copy link

With responsive design don't work:

<source :srcset="X" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment