Home >Web Front-end >Front-end Q&A >How to click a button in vue to display a picture

How to click a button in vue to display a picture

王林
王林Original
2023-05-08 09:12:072116browse

With the development of web applications, user interaction has become more and more important. Vue is a popular JavaScript framework for building modern web applications. In Vue, we can easily add interactivity, such as displaying an image when a button is clicked. This article will introduce how to implement this function in Vue.

First, we need to prepare a picture. Here we take a picture named "example.jpg" as an example. Place the image in the static folder of your Vue application (usually the "public" folder) like this:

/public/images/example.jpg

Next, we need to create a Vue component and add a button and An image element. In this example, we will use Vue Single File Component (SFC):

<template>
  <div>
    <button @click="showImage">显示图片</button>
    <img v-if="show" src="/images/example.jpg" alt="示例图片">
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    };
  },
  methods: {
    showImage() {
      this.show = true;
    }
  }
};
</script>

In this component, we define a data attribute "show", which is of type Boolean. At the beginning, we set its value to "false" because we don't want to display images by default. When the user clicks the button, we will call a method called "showImage" which will update the value of the data attribute "show" to display the image.

The picture element has a "v-if" directive added to show or hide it as needed. Its value is the expression "show", which means that the picture will only be displayed if "show" is true.

Finally, we export this component and include it in our Vue application. This can be done in a Vue instance as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Vue显示图片示例</title>
  </head>
  <body>
    <div id="app">
      <image-display></image-display>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="src/components/ImageDisplay.vue"></script>
    <script>
      new Vue({
        el: '#app',
        components: {
          'image-display': ImageDisplay
        }
      });
    </script>
  </body>
</html>

In this Vue instance, we include the component "image-display" in a div element called "app". We also imported our Vue component and passed it to the Vue instance as a property in the component options object to be loaded as part of the Vue application.

Now, when the user clicks the show image button, our Vue application will display the "example.jpg" image. This is a basic Vue interaction example that can help you add interactivity to your own web applications.

The above is the detailed content of How to click a button in vue to display a picture. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn