Guest User

Untitled

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <template>
  2. <div v-if="isLoading">loading....</div>
  3. <img v-else ref="image" :src="image" class="c-image" @error="onImageLoadError">
  4. </template>
  5.  
  6. <script>
  7.  
  8. export default {
  9. props: {
  10. src: String,
  11. placeholder: {
  12. type: String,
  13. default: require('@assets/images/Cacto.svg')
  14. }
  15. },
  16.  
  17. data () {
  18. return {
  19. image: '',
  20. isLoading: false
  21. }
  22. },
  23.  
  24. watch: {
  25. image: {
  26. immediate: true,
  27. handler: 'setImage'
  28. }
  29. },
  30.  
  31. methods: {
  32. onImageLoadError () {
  33. this.isLoading = true
  34.  
  35. this.$refs.image.src = this.placeholder
  36.  
  37. this.isLoading = false
  38. },
  39.  
  40. loadImage () {
  41. try {
  42. this.image = this.src || this.placeholder
  43. } catch (error) {
  44. this.image = this.placeholder
  45. }
  46. },
  47.  
  48. setImage () {
  49. this.isLoading = true
  50.  
  51. this.loadImage()
  52.  
  53. this.isLoading = false
  54. }
  55. }
  56. }
  57. </script>
Add Comment
Please, Sign In to add comment