How to use sweetalert2 pop-up plug-in in vue project

不言
Release: 2018-07-16 17:37:43
Original
4733 people have browsed it

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

1). Install sweetalert2

npm install sweetalert2@7.15.1 --save
Copy after login

2). Encapsulate sweetalert2

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 } }
Copy after login

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

3). Introduce and use the plug-in

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: '' })
Copy after login

4). Add exit confirmation

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') } }) }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!