1、效果

  1. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1225858/1599636768099-52e0ffac-6a49-4dea-a1c8-37e32fad293b.png#align=left&display=inline&height=381&margin=%5Bobject%20Object%5D&name=image.png&originHeight=762&originWidth=426&size=151145&status=done&style=none&width=213)

2、 参数

title 标题
text 正文
confirmTxt 确定文案
cancelTxt 取消文案

事件

onConfirm 点击确定执行的回调函数
onCancel 点击取消执行的回调函数

3、使用

main.js 注册

  1. import { Toast, Confirm } from '/Confirm';
  2. Vue.use(Confirm);

page

  1. this.$confirm({
  2. text: '是否确认,授权天猫精灵,提供“您的联系方式”给红外码库厂商以便与您联系?',
  3. cancelTxt: '取消',
  4. confirmTxt: '确认',
  5. onConfirm: function() {},
  6. onCancel: function() {
  7. that.isChoose = false
  8. }
  9. })

4、插件代码

Confirm/index.ts

  1. import ConfirmVue from './src/confirm.vue'
  2. let instance
  3. let Confirm = {
  4. install(Vue) {
  5. Vue.prototype.$confirm = function (opts) {
  6. let defaultOpts = {
  7. title: '',
  8. text: '',
  9. confirmTxt: '确定',
  10. cancelTxt: '取消'
  11. }
  12. if (!instance) {
  13. instance = new ConfirmVue({
  14. el: document.createElement('div')
  15. })
  16. }
  17. // let options = Object.assign(defaultOpts, opts)
  18. // Object.assign(instance, options)
  19. for(var key in opts){
  20. if(opts.hasOwnProperty(key) === true){
  21. defaultOpts[key]=opts[key];
  22. }
  23. }
  24. var options = defaultOpts
  25. for(var key in options){
  26. if(options.hasOwnProperty(key) === true){
  27. instance[key]=options[key];
  28. }
  29. }
  30. document.body.appendChild(instance.$el)
  31. Vue.nextTick(() => {
  32. instance.show = true
  33. })
  34. }
  35. }
  36. }
  37. export default Confirm

Confirm/src/confirm.vue

  1. <template>
  2. <div class="tn-confirm" v-show="show" @touchmove.stop.prevent>
  3. <div class="tn-confirm__mask"></div>
  4. <div class="tn-confirm__wrapper" @touchmove.stop.prevent>
  5. <div class="tn-confirm__title">{{title}}</div>
  6. <div class="tn-confirm__text">{{text}}</div>
  7. <div class="tn-confirm__button">
  8. <div class="tn-confirm__button-cancel" @click="onCancelClick">{{cancelTxt}}</div>
  9. <div class="tn-confirm__button-confirm" @click="onConfirmClick">{{confirmTxt}}</div>
  10. </div>
  11. </div>
  12. </div>
  13. </template>
  14. <script lang="ts">
  15. import Vue, { PropOptions } from 'vue'
  16. export default Vue.extend({
  17. name: 'TnConfirm',
  18. componentName: 'TnConfirm',
  19. data() {
  20. return {
  21. show: false
  22. }
  23. },
  24. props: {
  25. text: String,
  26. title: String,
  27. confirmTxt: String,
  28. cancelTxt: String,
  29. onConfirm: {
  30. type: Function
  31. } as PropOptions<any>,
  32. onCancel: {
  33. type: Function
  34. } as PropOptions<any>
  35. },
  36. methods: {
  37. onCancelClick() {
  38. this.show = false
  39. this.onCancel && this.onCancel()
  40. },
  41. onConfirmClick() {
  42. this.show = false
  43. this.onConfirm && this.onConfirm()
  44. }
  45. }
  46. })
  47. </script>
  48. <style lang="stylus">
  49. @import '../../../style/confirm.styl'
  50. </style>

confirm.styl

  1. .tn-confirm
  2. &__wrapper
  3. top 50%
  4. left 50%
  5. position fixed
  6. display flex
  7. flex-direction column
  8. align-items center
  9. transform translate(-50%, -50%)
  10. width 270px
  11. border-radius 12px
  12. background #fcfcfc
  13. box-sizing border-box
  14. text-align center
  15. z-index 10000
  16. &__title
  17. max-width 213px
  18. margin-top 20px
  19. color #333
  20. font-size 17px
  21. line-height 24px
  22. &__text
  23. color #333
  24. margin-top 10px
  25. font-size 13px
  26. max-width 143px
  27. line-height 18px
  28. &__button
  29. display flex
  30. flex-direction row
  31. width 100%
  32. margin-top 20px
  33. height 45px
  34. line-height 45px
  35. color #007AFF
  36. font-size 17px
  37. border-top 1px solid rgb(235, 235, 235)
  38. &-cancel
  39. flex 1
  40. &-confirm
  41. border-left 1px solid rgb(235, 235, 235)
  42. flex 1
  43. &__mask
  44. background rgba(0, 0, 0, 0.4)
  45. position fixed
  46. margin 0
  47. top 0
  48. right 0
  49. bottom 0
  50. left 0
  51. z-index 10000
  52. &__text
  53. font-size 10px
  54. margin-top 5px
  55. display inline-block