How to implement a prompt-like pop-up box in Vue?
In front-end development, pop-up boxes are very common components, especially pop-up boxes similar to prompt. The Vue framework provides us with many components, but there is no component that directly implements the prompt pop-up box. So how to implement a pop-up box similar to prompt in Vue? This article will briefly introduce several implementation methods.
Method 1: Use Vue’s own $emit
In Vue, each Vue instance has a built-in $emit method. You can use it to customize events and listen for this event in the component. We can use this feature to implement pop-up boxes.
First, in the component that needs to pop up the pop-up box, you can use $emit to customize an event. In this event, you can pass the parameters required by the pop-up box:
Vue.component('prompt', { template: ` <div> <div v-if="visible" class="mask"> <div class="prompt-box"> <h3 id="title">{{title}}</h3> <input type="text" v-model="inputValue"> <button @click="close('cancel')">取消</button> <button @click="close('ok')">确定</button> </div> </div> </div> `, props: { title: { type: String, default: '请输入' }, visible: { type: Boolean, default: false } }, data() { return { inputValue: '' } }, methods: { close(val) { if (val === 'ok') { this.$emit('ok', this.inputValue) } else { this.$emit('cancel') } } } })
Then in the parent component, Listen to this custom event and use v-model to two-way bind the value of the input box:
<template> <div> <button @click="showPrompt = true">弹出提示框</button> <prompt title="请输入内容" :visible="showPrompt" @cancel="showPrompt = false" @ok="handleOk"></prompt> </div> </template> <script> import Prompt from './prompt.vue' export default { components: { Prompt }, data() { return { showPrompt: false, inputValue: '' } }, methods: { handleOk(val) { this.showPrompt = false this.inputValue = val } } } </script>
Method 2: Use Vuex state management
If the pop-up box needs to be used in multiple components , and many parameters need to be passed, which can be achieved using Vuex state management.
First, add a new state in Vuex to store the status and parameters of the pop-up box:
state: { isPromptShow: false, promptData: { title: '', inputPlaceholder: '', inputValue: '' } }
Then, in the component that needs to pop up the pop-up box, use the $store.commit method to modify it isPromptShow and promptData status:
methods: { showPrompt() { this.$store.commit('setPromptData', { isPromptShow: true, promptData: { title: '请输入', inputPlaceholder: '请输入内容', inputValue: '' } }) } }
In the component of the pop-up box, you can use mapState and mapMutations to obtain the status in Vuex and submit mutations, thereby realizing the event response logic of the pop-up box:
<template> <div v-if="isPromptShow" class="mask"> <div class="prompt-box"> <h3 id="promptData-title">{{promptData.title}}</h3> <input type="text" :placeholder="promptData.inputPlaceholder" v-model="promptData.inputValue"> <button @click="closePrompt('cancel')">取消</button> <button @click="closePrompt('ok')">确定</button> </div> </div> </template> <script> import { mapState, mapMutations } from 'vuex' export default { computed: { ...mapState(['isPromptShow', 'promptData']) }, methods: { ...mapMutations({ setPromptData: 'SET_PROMPT_DATA' }), closePrompt(val) { if (val === 'ok') { this.$emit('ok', this.promptData.inputValue) } else { this.$emit('cancel') } this.setPromptData({ isPromptShow: false }) } } } </script>
Change the state of isPromptShow in vuex
mutations: { SET_PROMPT_DATA(state, data) { state.isPromptShow = data.isPromptShow state.promptData = data.promptData } }
In the parent component, you not only need to use Vuex's mapState to obtain isPromptShow and promptData, but also need to use mapActions to call the method of submitting mutations in Vuex:
<template> <div> <button @click="showPrompt">弹出提示框</button> <prompt v-if="isPromptShow" @ok="handleOk" @cancel="closePrompt"></prompt> </div> </template> <script> import Prompt from './prompt.vue' import { mapState, mapActions } from 'vuex' export default { components: { Prompt }, computed: { ...mapState(['isPromptShow', 'promptData']) }, methods: { ...mapActions(['setPromptData']), showPrompt() { this.setPromptData({ isPromptShow: true, promptData: { title: '请输入', inputPlaceholder: '请输入内容', inputValue: '' } }) }, closePrompt() { this.setPromptData({ isPromptShow: false }) }, handleOk(val) { console.log(val) this.setPromptData({ isPromptShow: false }) } } } </script>
Method 3: Use a third-party library
If you think the above two methods are too troublesome, you can actually consider using a third-party library to implement a prompt-like pop-up box. Currently popular third-party libraries include ElementUI, Vuetify, iView, etc. These libraries provide pop-up components similar to prompt, which are very practical.
For example, there is a MessageBox pop-up box in the ElementUI component library:
this.$confirm('内容', 'title', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { // 点击确定回调 }).catch(() => { // 点击取消回调 })
Summary
This article mainly introduces three methods to implement a prompt-like pop-up box in Vue. They are using $emit, using Vuex state management and using third-party libraries. Each method has its own characteristics and applicable scenarios, and needs to be selected according to specific circumstances.
The above is the detailed content of How to implement a prompt-like popup box in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix mainly considers performance, scalability, development efficiency, ecosystem, technical debt and maintenance costs in framework selection. 1. Performance and scalability: Java and SpringBoot are selected to efficiently process massive data and high concurrent requests. 2. Development efficiency and ecosystem: Use React to improve front-end development efficiency and utilize its rich ecosystem. 3. Technical debt and maintenance costs: Choose Node.js to build microservices to reduce maintenance costs and technical debt.

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.