main.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <basic-container>
  3. <iframe :src="src"
  4. class="iframe"
  5. ref="iframe"></iframe>
  6. </basic-container>
  7. </template>
  8. <script>
  9. import { mapGetters } from "vuex";
  10. import NProgress from "nprogress"; // progress bar
  11. import "nprogress/nprogress.css"; // progress bar style
  12. export default {
  13. name: "AvueIframe",
  14. data() {
  15. return {
  16. urlPath: this.getUrlPath() //iframe src 路径
  17. };
  18. },
  19. created() {
  20. NProgress.configure({ showSpinner: false });
  21. },
  22. mounted() {
  23. this.load();
  24. this.resize();
  25. },
  26. props: ["routerPath"],
  27. watch: {
  28. $route: function() {
  29. this.load();
  30. },
  31. routerPath: function() {
  32. // 监听routerPath变化,改变src路径
  33. this.urlPath = this.getUrlPath();
  34. }
  35. },
  36. computed: {
  37. ...mapGetters(["screen"]),
  38. src() {
  39. return this.$route.query.src
  40. ? this.$route.query.src.replace("$", "#")
  41. : this.urlPath;
  42. }
  43. },
  44. methods: {
  45. // 显示等待框
  46. show() {
  47. NProgress.start();
  48. },
  49. // 隐藏等待狂
  50. hide() {
  51. NProgress.done();
  52. },
  53. // 加载浏览器窗口变化自适应
  54. resize() {
  55. window.onresize = () => {
  56. this.iframeInit();
  57. };
  58. },
  59. // 加载组件
  60. load() {
  61. this.show();
  62. var flag = true; //URL是否包含问号
  63. if (this.$route.query.src.indexOf("?") == -1) {
  64. flag = false;
  65. }
  66. var list = [];
  67. for (var key in this.$route.query) {
  68. if (key != "src" && key != "name" && key != "i18n") {
  69. list.push(`${key}= this.$route.query[key]`);
  70. }
  71. }
  72. list = list.join("&").toString();
  73. if (flag) {
  74. this.$route.query.src = `${this.$route.query.src}${
  75. list.length > 0 ? `&list` : ""
  76. }`;
  77. } else {
  78. this.$route.query.src = `${this.$route.query.src}${
  79. list.length > 0 ? `?list` : ""
  80. }`;
  81. }
  82. //超时3s自动隐藏等待狂,加强用户体验
  83. let time = 3;
  84. const timeFunc = setInterval(() => {
  85. time--;
  86. if (time == 0) {
  87. this.hide();
  88. clearInterval(timeFunc);
  89. }
  90. }, 1000);
  91. this.iframeInit();
  92. },
  93. //iframe窗口初始化
  94. iframeInit() {
  95. const iframe = this.$refs.iframe;
  96. const clientHeight =
  97. document.documentElement.clientHeight - (screen > 1 ? 200 : 130);
  98. if (!iframe) return;
  99. iframe.style.height = `${clientHeight}px`;
  100. if (iframe.attachEvent) {
  101. iframe.attachEvent("onload", () => {
  102. this.hide();
  103. });
  104. } else {
  105. iframe.onload = () => {
  106. this.hide();
  107. };
  108. }
  109. },
  110. getUrlPath: function() {
  111. //获取 iframe src 路径
  112. let url = window.location.href;
  113. url = url.replace("/myiframe", "");
  114. return url;
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="scss">
  120. .iframe {
  121. width: 100%;
  122. height: 100%;
  123. border: 0;
  124. overflow: hidden;
  125. box-sizing: border-box;
  126. }
  127. </style>