Home > Article > Web Front-end > How to use sweetalert2 pop-up plug-in in vue project
This article mainly introduces how to use the sweetalert2 pop-up plug-in in the vue project. It has certain reference value. Now I share it with you. Friends in need can refer to it
npm install sweetalert2@7.15.1 --save
in src
Create a new plugins
folder, and then create a new vue-sweetalert2.js
file, copy and paste the following code:
src/plugins/vue-sweetalert2.js
import swal from 'sweetalert2' export default { install: (Vue) => { // sweetalert2 的设置默认配置的方法 swal.setDefaults({ type: 'warning', showCancelButton: true, confirmButtonColor: 'rgb(140,212,245)', cancelButtonColor: 'rgb(193,193,193)' }) // 添加全局方法 Vue.swal = swal // 添加实例方法 Vue.prototype.$swal = swal } }
Here we encapsulate sweetalert2 into a plug-in, a plug-in for Vue.js There is a public method install
. The first parameter of this method is the Vue constructor. After adding swal
as a global method and instance method, we can access it through Vue.swal
and this.$swal
Open the src/main.js
file, introduce and use ./plugins/vue-sweetalert2
(single-line comment part is the modification involved):
src/main.js
import Vue from 'vue' import App from './App' import router from './router' import './directives' import './components' import store from './store' // 引入插件 import VueSweetalert2 from './plugins/vue-sweetalert2' // 使用插件 Vue.use(VueSweetalert2) Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, components: { App }, template: '<App/>' })
Open src /components/layouts/TheEntry.vue
file, modify logout
method:
src/components/layouts/TheEntry.vue
logout() { this.$swal({ text: '你确定要退出吗?', confirmButtonText: '退出' }).then((res) => { if (res.value) { this.$store.dispatch('logout') } }) }
Related recommendations:
How to use Element form validation in vue
Layer pop-up plug-in usage tutorial
The above is the detailed content of How to use sweetalert2 pop-up plug-in in vue project. For more information, please follow other related articles on the PHP Chinese website!