Home > Web Front-end > JS Tutorial > How to use sweetalert2 pop-up plug-in in vue project

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

不言
Release: 2018-07-16 17:37:43
Original
4843 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

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

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

3). Introduce and use the plug-in

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: &#39;<App/>&#39;
})
Copy after login

4). Add exit confirmation

Open src /components/layouts/TheEntry.vue file, modify logout method:

src/components/layouts/TheEntry.vue

logout() {
  this.$swal({
    text: &#39;你确定要退出吗?&#39;,
    confirmButtonText: &#39;退出&#39;
  }).then((res) => {
    if (res.value) {
      this.$store.dispatch(&#39;logout&#39;)
    }
  })
}
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template