Last active
July 12, 2018 22:08
-
-
Save dimensi/485a5c28fd76b2930eeaba56fc75a42a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const waitImage = ref => | |
| new Promise((resolve, reject) => { | |
| ref.onload = (event) => { resolve(event); ref.onload = null }; | |
| ref.onerror = (event) => { reject(event); ref.onerror = null }; | |
| }); | |
| const createImage = str => Object.assign(document.createElement('img'), { src: str }); | |
| const waitImages = arrStr => Promise.all(arrStr.map(str => waitImage(createImage(str)))); | |
| const images = Array.from({ length: 10 }, (_, index) => `https://source.unsplash.com/random?v=${index}`) | |
| export default { | |
| data: () => ({ isLoading: true }), | |
| async mounted() { | |
| await waitImages(images) | |
| this.isLoading = false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <img v-for="img in imgs" :src="img.src" :key="img.id" ref="images"/> | |
| </template> | |
| <script> | |
| const waitImage = ref => | |
| new Promise((resolve, reject) => { | |
| ref.onload = (event) => { resolve(event); ref.onload = null }; | |
| ref.onerror = (event) => { reject(event); ref.onerror = null }; | |
| }); | |
| const waitImages = arrImgs => Promise.all(arrImgs.map(waitImage)); | |
| export default { | |
| props: { | |
| imgs: Array, | |
| }, | |
| data: () => ({ isLoading: true }), | |
| async mounted() { | |
| await waitImages(this.$refs.images) | |
| this.isLoading = false; | |
| } | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment