vue2.0+ 从插件开发到npm发布的示例代码

2020-06-13 10:16:10易采站长站整理

status: true
}
},
methods: {
val2input(item) {
this.val.push(item)
if (this.val.length == this.pwdLength) {
//打开等待层
this.status = false
//输入完毕后将值传递给父组件
this.$emit('inputDown', this.val.join(''))
//清空数据
this.val = [] }
},
delVal () {
if (this.val.length > 0) this.val.pop()
},
closePay () {
this.payPopOptions.isShow = false;
},
$payStatus(flag = false) {
const that = this
//模拟等候feel
setTimeout(() => {
if (flag) {
//成功
this.loadingTxt = this.successTxt
//关闭输入层,重置等待语
setTimeout(function() {
that.payPopOptions.isShow = !flag
that.status = true
that.loadingTxt = that.payPopOptions.loadingTxt || '请稍候...'
}, this.resultTime)
} else {
//失败
this.loadingTxt = this.failTxt
//重新打开输入层,重置等待语
setTimeout(function() {
that.status = true
that.loadingTxt = that.payPopOptions.loadingTxt || '请稍候...'
}, this.resultTime)
}
}, this.loadingTime)
}
}
}

基本源码都在这里了,实现方法比较简单,这里就不多过介绍了…

3.尝试使用

我们先尝试在本地app.vue中使用


<div id="app">
<div @click="showPayPop">点击弹出支付框</div>
<vue-pay-pop ref="pay" :payPopOptions="payPopOptions" @inputDown="inputDown"></vue-pay-pop>
</div>


import vuePayPop from './lib/vue-pay-pop'

export default {
name: 'app',
data () {
return {
payPopOptions: {
isShow: false
},
}
},
components: {
vuePayPop
},
methods: {
inputDown(val) {
//模拟检查数据
setTimeout(() => {
if (val == '111111') {
this.$refs.pay.$payStatus(true)
} else {
this.$refs.pay.$payStatus(false)
}
}, 1000)
},
showPayPop() {
this.payPopOptions.isShow = true;
}
}
}

其中payPopOptions中isShow是必传项,用来控制弹出框的显隐

其他更多参数为可选参数

4.更改配置文件

ok,现在我们去更改配置文件,为我们的发布做准备

index.js


import vuePayPop from './vue-pay-pop.vue'

const PayPop = {
install(Vue, options) {
Vue.component(vuePayPop.name, vuePayPop)
}
}