提示的消息方法大概有两种:自定义弹框、原生弹框。显而易见自带的弹框的用户体验是很差的(尤其是再移动端),废话不多说,开始编写自定义我们的弹框吧~

功能分析:

通过封装 alert 可以封装成两种类型弹框:

  • 带有确定按钮的单纯的弹框
  • 带有确定和取消按钮的功能性弹框

    构建思路:

  • 通过外部传入类型(type)来判断是普通弹框还是功能性弹框

  • 提示的信息一定是外部所传递进来的(msg)
  • 外部传入是否让它显示(isShow)

结构**与样式如下:**

  1. <template>
  2. <div v-show="isShow">
  3. <div class="alert">
  4. <div class="flex">{{ msg }}</div>
  5. <div v-if="type === 'alert'">
  6. <div class="btnCommon success" @click="close()">确定</div>
  7. </div>
  8. <div v-else class="space-round">
  9. <div class="btnCommon cancel" @click="cancelEvent()">取消</div>
  10. <div class="btnCommon success" @click="successEvent()">确定</div>
  11. </div>
  12. </div>
  13. <div class="mask" @click="closeMask()"></div>
  14. </div>
  15. </template>
  16. <style lang="scss" scoped>
  17. $btn-main: #009688;
  18. $btn-dark: darken($btn-main, 5%);
  19. .alert {
  20. width: 300px;
  21. height: 150px;
  22. position: fixed;
  23. top: 50%;
  24. left: 50%;
  25. z-index: 300;
  26. border-radius: 6px;
  27. background-color: #fff;
  28. transform: translate(-50%, -50%);
  29. padding: 20px 10px;
  30. box-shadow: 0 5px 8px rgba(0, 0, 0, 0.05);
  31. display: flex;
  32. flex-flow: column nowrap;
  33. justify-content: center;
  34. align-items: center;
  35. }
  36. .flex {
  37. flex: 1;
  38. display: flex;
  39. align-items: center;
  40. }
  41. .space-round {
  42. display: flex;
  43. flex-flow: row nowrap;
  44. justify-content: space-around;
  45. align-items: center;
  46. width: 100%;
  47. padding: 0 10px;
  48. }
  49. .btnCommon {
  50. width: 105px;
  51. height: 32px;
  52. text-align: center;
  53. line-height: 32px;
  54. border-radius: 6px;
  55. cursor: pointer;
  56. &.success {
  57. color: #fff;
  58. background-color: $btn-main;
  59. &:hover {
  60. transition: 0.2s;
  61. background-color: $btn-dark;
  62. }
  63. }
  64. &.cancel {
  65. color: #333;
  66. background-color: #ededed;
  67. }
  68. }
  69. .mask {
  70. width: 100%;
  71. height: 100%;
  72. position: fixed;
  73. top: 0;
  74. left: 0;
  75. z-index: 299;
  76. background-color: rgba(0, 0, 0, 0.4);
  77. overflow: hidden;
  78. }
  79. </style>

逻辑(JS)部分如下:

  1. export default {
  2. props: {
  3. type: {
  4. type: String,
  5. default: 'alert'
  6. },
  7. msg: {
  8. type: String,
  9. default: ''
  10. },
  11. isShow: {
  12. type: Boolean,
  13. default: false
  14. },
  15. success: {
  16. type: Function,
  17. default: () => {
  18. console.log('点击了success')
  19. }
  20. },
  21. cancel: {
  22. type: Function,
  23. default: () => console.log('点击了cancel')
  24. }
  25. },
  26. methods: {
  27. close() {
  28. // 通过原型链改变 isShow,将其隐藏
  29. window.vue.$alert('', false)
  30. },
  31. closeMask() {
  32. if (this.type === 'alert') {
  33. this.close()
  34. }
  35. },
  36. cancelEvent() {
  37. this.cancel()
  38. this.close()
  39. },
  40. successEvent() {
  41. this.success()
  42. this.close()
  43. }
  44. },
  45. mounted() {
  46. window.vue = this
  47. }
  48. }

开发插件:

  1. import AlertComponent from './Alert.vue'
  2. const Alert = {}
  3. Alert.install = Vue => {
  4. // 创建一个具有 alert.vue 的 Vue构造器
  5. const AlertConstructor = Vue.extend(AlertComponent)
  6. const instance = new AlertConstructor()
  7. // 将实例挂载到一个新创建的 div 上
  8. instance.$mount(document.createElement('div'))
  9. // 插入到body的最下面
  10. document.body.appendChild(instance.$el)
  11. // 添加实例方法
  12. Vue.prototype.$alert = (msg, bol) => {
  13. instance.type = 'alert'
  14. instance.msg = msg
  15. instance.isShow = bol === undefined ? true : bol
  16. }
  17. Vue.prototype.$confirm = (msg, success, cancel, bol) => {
  18. instance.type = 'confirm'
  19. instance.msg = msg
  20. instance.isShow = bol === undefined ? true : bol
  21. if (typeof success !== 'undefined') {
  22. instance.success = success
  23. }
  24. if (typeof cancel !== 'undefined') {
  25. instance.cancel = cancel
  26. }
  27. }
  28. }
  29. export default Alert

全局使用插件:

  1. // main.js
  2. ...
  3. import Alert from './components/modules/alert'
  4. Vue.use(Alert)
  5. ...

使用示例:

  1. if (data.code === 500) {
  2. this.$alert('用户名密码校验失败,请检查!')
  3. } else {
  4. this.$alert('服务器错误!')
  5. }