index.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Popup } from "vant";
  2. import { defineComponent } from "vue";
  3. import qs from 'query-string';
  4. export default defineComponent({
  5. name: 'col-popup',
  6. props: {
  7. popupStatus: {
  8. type: Boolean,
  9. default: false
  10. }
  11. },
  12. data() {
  13. return {
  14. popupShow: false,
  15. }
  16. },
  17. watch: {
  18. popupStatus(val) {
  19. this.hashState()
  20. }
  21. },
  22. mounted() {
  23. window.addEventListener('hashchange', this.onHash, false)
  24. },
  25. unmounted() {
  26. window.removeEventListener('hashchange', this.onHash, false)
  27. },
  28. methods: {
  29. onHash() {
  30. this.$emit("update:popupStatus", false);
  31. },
  32. onPopupClose(val: boolean) {
  33. this.$emit("update:popupStatus", val);
  34. this.hashState();
  35. },
  36. hashState() {
  37. // 打开弹窗
  38. if (this.popupStatus) {
  39. const splitUrl = window.location.hash.slice(1).split('?');
  40. const query = qs.parse(splitUrl[1]);
  41. let times = 0
  42. for (let key in query) {
  43. times++
  44. }
  45. const origin = window.location.href
  46. const url = times > 0 ? '&cPop=' + (+new Date()) : '?cPop=' + (+new Date())
  47. history.pushState("", "", `${origin}${url}`)
  48. } else {
  49. const splitUrl = window.location.hash.slice(1).split('?');
  50. const query = qs.parse(splitUrl[1]);
  51. if (query.cPop) {
  52. window.history.go(-1)
  53. }
  54. }
  55. if (this.$refs.protocolPopup) {
  56. (this.$refs.protocolPopup as any).scrollTop = 0
  57. }
  58. }
  59. },
  60. render() {
  61. return (
  62. <Popup ref="protocolPopup" show={this.popupStatus} transitionAppear={true} position="bottom" style={{ height: '100%' }}>
  63. {this.$slots.default && this.$slots.default()}
  64. </Popup>
  65. )
  66. }
  67. })