index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { computed, ref } from 'vue'
  2. import { mount } from '../../utils/componentHelper'
  3. import { toRawType } from '../../utils/index'
  4. import Message from './message.vue'
  5. const types = ['success', 'warning', 'error']
  6. Message.use = function use(app) {
  7. const indexs = ref([])
  8. Message.show = function (config) {
  9. if (toRawType(config) === 'String') {
  10. config = { msg: config }
  11. }
  12. config.time = config.time || 3000
  13. config.type = types.includes(config.type) ? config.type : types[0]
  14. const instance = ref(null)
  15. const index = computed(() => (instance.value ? indexs.value.indexOf(instance) : 0))
  16. const hide = () => {
  17. instance.value.destroy()
  18. indexs.value = indexs.value.filter(i => i !== instance.value)
  19. }
  20. instance.value = mount(Message, {
  21. app,
  22. props: {
  23. ...config,
  24. index,
  25. destroy: hide,
  26. },
  27. })
  28. indexs.value.push(instance)
  29. }
  30. for (const type of types) {
  31. Message[type] = config => {
  32. if (toRawType(config) === 'String') {
  33. config = {
  34. msg: config,
  35. type,
  36. }
  37. }
  38. return Message.show(config)
  39. }
  40. }
  41. }
  42. export default Message