Thoughts
Create an array, put pictures in the array, use props (parent component to pass value to child component), v-for (loop), v-bind (binding attribute) to pass the picture into the div defined by HTML.
Ⅰ. Import the vue file in the head (the import premise is that the vue file has been introduced into js)
<script src="js/vue.js"></script>
Ⅱ. Create a z-div in HTML (you can name it according to your preferences) to receive the passed value of the component, and use v-for to make the array elements traverse the loop to display the picture, v -bind binds im (im is defined in the global component in the script tag), and the variable i is passed into im.
<div id="app"> <z-div v-for="i in img" :im="i"></z-div> </div>
Ⅲ. Define a component , write the data that needs to be passed to z-div in it, bind the src element with v-bind, and accept the passed value of the component.
<template id="imgs"> <div id="box" @click="change"> <img :src="im" alt="" v-show="show"> </div> </template>
Ⅳ. Define a global component in the script tag (the global component must be registered before creating the Vue instance), and use the props attribute to define an im (the im in the array is a variable bound through the property, bound to the child component).
Vue.component( 'z-div', { template: '#imgs', props: [ 'im' ], data: function () { return { show: true } }, methods: { change: function () { this.show = !this.show } } } )
Ⅴ. Define a new Vue, and define an array in it, and put the pictures we need in it.
var vm = new Vue({ el: '#app', data: { img: [ 'img/222_01.jpg', 'img/222_02.jpg', 'img/222_03.jpg', 'img/222_04.jpg', 'img/222_05.jpg', 'img/222_06.jpg', 'img/222_07.jpg', 'img/222_08.jpg', 'img/222_09.jpg', 'img/222_10.jpg', 'img/222_11.jpg', 'img/222_12.jpg', 'img/222_13.jpg', 'img/222_14.jpg', 'img/222_15.jpg', 'img/222_16.jpg', 'img/222_17.jpg', 'img/222_18.jpg', 'img/222_19.jpg', 'img/222_20.jpg', 'img/222_21.jpg', 'img/222_22.jpg', 'img/222_23.jpg', 'img/222_24.jpg', 'img/222_25.jpg' ] } } )
Ⅵ.Edit style
* { margin: 0; padding: 0; } #app { width: 550px; margin: 0 auto; display: flex; flex-wrap: wrap; justify-content: space-around; } img { width: 108px; height: 138px; } #box { width: 108px; height: 138px; background-color: pink; margin: 1px 0px; }
The effect picture is as follows
Full code
// import vue <script src="js/vue.js"></script> //v-for defines the loop, v-bind binds attributes
Note: If you want the original picture not to be displayed, click to display the picture effect, and change the value of show in the position below to false