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
insrcCreate a newpluginsfolder, and then create a newvue-sweetalert2.jsfile, 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 methodinstall. The first parameter of this method is the Vue constructor. After addingswalas a global method and instance method, we can access it throughVue.swalandthis.$swal
Open thesrc/main.jsfile, 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: ' ' })
Opensrc /components/layouts/TheEntry.vuefile, modifylogoutmethod:
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!
What are the common linux systems?
What are the applications of the Internet of Things?
Quick shutdown shortcut key
Build your own git server
What are the javabean attributes?
http 400 Bad Request
What are the reasons why a mobile phone has an empty number?
How to watch live broadcast playback records on Douyin