目录结构:
—elert
—|— elert.vue
—|— elert.js
elert.vue代码:
<template>
<div class="modal fade" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{message}}</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<div class="text-danger">{{message}}</div>
<div>{{message}}</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" data-dismiss="modal" >确定</button>
</div>
</div>
</div>
</div>
</template>
<script>
import $ from 'jquery'
export default {
name: "Toast",
data(){
return {
isShow:false,
message:''
}
},
methods:{
show(message){
this.message = message
$(this.$el).modal('show')
}
}
}
</script>
elert.js代码:
import alert from "./alert.vue"
var AlertPlugin = {
install(Vue) {
const ToastConstructor = Vue.extend(alert)
const alert = new ToastConstructor()
alert.$mount(document.createElement('div'))
document.body.appendChild(alert.$el)
Vue.prototype.$alert= alert
}
}
export default AlertPlugin
main.js引入:
import AlertPlugin from './components/alert/alert'
Vue.use(AlertPlugin)
vue内使用方法:
this.$alert.show() //show是alert.vue内的方法