vue封装可复用组件confirm,并绑定在vue原型上的示例

2020-06-13 06:01:54易采站长站整理

// => 在main.js里面先引入 import theConfirm from './components/confirm/confirm.js'
// => 再挂载 Vue.prototype.$confirm = theConfirm;
//在需要的地方直接用以下方法调用即可:
// this.$confirm({
// type:'提示',
// msg:'是否删除这条信息?',
// btn:{
// ok:'yes',
// no:'no'
// }
// }).then(() => {
// console.log('ok')
// })
// .catch(() => {
// console.log('no')
// })

main.js


// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

//这里就是对组件的绑定
import theConfirm from './components/confirm/confirm.js'
Vue.prototype.$confirm = theConfirm;

Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})

helloworld.vue


<template>
<div class="hello">
<span @click="handMe()">点击一下</span>
</div>
</template>

<script>
export default {
name: 'HelloWorld',
data () {
return {

}
},
methods:{
handMe(){
this.$confirm({
type:'提示',
msg:'是否删除这条信息?',
btn:{
ok:'yes',
no:'no'
}
}).then(() => {
console.log('ok')
})
.catch(() => {
console.log('no')
})
}
}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
span{font-size: 24px;}
</style>

以上这篇vue封装可复用组件confirm,并绑定在vue原型上的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。