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>显示图片</button> <img src="/static/imghwm/default1.png" data-src="/images/example.jpg" class="lazy" v-if="show" 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:
<title>Vue显示图片示例</title> <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>
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!

React'slimitationsinclude:1)asteeplearningcurveduetoitsvastecosystem,2)SEOchallengeswithclient-siderendering,3)potentialperformanceissuesinlargeapplications,4)complexstatemanagementasappsgrow,and5)theneedtokeepupwithitsrapidevolution.Thesefactorsshou

Reactischallengingforbeginnersduetoitssteeplearningcurveandparadigmshifttocomponent-basedarchitecture.1)Startwithofficialdocumentationforasolidfoundation.2)UnderstandJSXandhowtoembedJavaScriptwithinit.3)Learntousefunctionalcomponentswithhooksforstate

ThecorechallengeingeneratingstableanduniquekeysfordynamiclistsinReactisensuringconsistentidentifiersacrossre-rendersforefficientDOMupdates.1)Usenaturalkeyswhenpossible,astheyarereliableifuniqueandstable.2)Generatesynthetickeysbasedonmultipleattribute

JavaScriptfatigueinReactismanageablewithstrategieslikejust-in-timelearningandcuratedinformationsources.1)Learnwhatyouneedwhenyouneedit,focusingonprojectrelevance.2)FollowkeyblogsliketheofficialReactblogandengagewithcommunitieslikeReactifluxonDiscordt

TotestReactcomponentsusingtheuseStatehook,useJestandReactTestingLibrarytosimulateinteractionsandverifystatechangesintheUI.1)Renderthecomponentandcheckinitialstate.2)Simulateuserinteractionslikeclicksorformsubmissions.3)Verifytheupdatedstatereflectsin

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
