App.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div id="app">
  3. <router-view v-if="isRouterAlive" />
  4. </div>
  5. </template>
  6. <script>
  7. import { on, off } from "view-design/src/utils/dom";
  8. import { setMatchMedia } from "view-design/src/utils/assist";
  9. import { mapMutations } from "vuex";
  10. setMatchMedia();
  11. export default {
  12. name: "app",
  13. provide (){
  14. return {
  15. reload:this.reload
  16. }
  17. },
  18. data(){
  19. return {
  20. isRouterAlive: true
  21. }
  22. },
  23. methods: {
  24. ...mapMutations("store/layout", ["setDevice"]),
  25. handleWindowResize() {
  26. this.handleMatchMedia();
  27. },
  28. handleMatchMedia() {
  29. const matchMedia = window.matchMedia;
  30. if (matchMedia("(max-width: 600px)").matches) {
  31. var deviceWidth =
  32. document.documentElement.clientWidth || window.innerWidth;
  33. let css = "calc(100vw/7.5)";
  34. document.documentElement.style.fontSize = css;
  35. this.setDevice("Mobile");
  36. } else if (matchMedia("(max-width: 992px)").matches) {
  37. document.documentElement.style.fontSize = "12px";
  38. this.setDevice("Tablet");
  39. } else {
  40. document.documentElement.style.fontSize = "12px";
  41. this.setDevice("Desktop");
  42. }
  43. },
  44. reload(){
  45. this.isRouterAlive = false
  46. this.$nextTick(function () {
  47. this.isRouterAlive = true
  48. })
  49. }
  50. },
  51. mounted() {
  52. on(window, "resize", this.handleWindowResize);
  53. this.handleMatchMedia();
  54. },
  55. beforeDestroy() {
  56. off(window, "resize", this.handleWindowResize);
  57. },
  58. };
  59. </script>
  60. <style lang="less">
  61. .size {
  62. width: 100%;
  63. height: 100%;
  64. }
  65. html,
  66. body {
  67. .size;
  68. // overflow: hidden;
  69. margin: 0;
  70. padding: 0;
  71. }
  72. #app {
  73. .size;
  74. }
  75. </style>