将iframe页面的展示模式改为可以缓存的方式

This commit is contained in:
神话
2022-05-01 16:49:08 +08:00
parent 9a40a5148e
commit 9c45cdfa91
3 changed files with 71 additions and 14 deletions

View File

@@ -21,14 +21,6 @@
created () { created () {
this.goUrl() this.goUrl()
}, },
updated () {
this.goUrl()
},
watch: {
$route(to, from) {
this.goUrl();
}
},
methods: { methods: {
goUrl () { goUrl () {
let url = this.$route.meta.url let url = this.$route.meta.url

View File

@@ -25,6 +25,13 @@
<router-view v-if="reloadFlag"/> <router-view v-if="reloadFlag"/>
</template> </template>
</transition> </transition>
<!-- iframe页 -->
<component
v-for="item in hasOpenComponentsArr"
:key="item.name"
:is="item.name"
v-show="$route.path === item.path">
</component>
</div> </div>
</global-layout> </global-layout>
</template> </template>
@@ -37,6 +44,7 @@
const indexKey = '/dashboard/analysis' const indexKey = '/dashboard/analysis'
import Vue from 'vue' import Vue from 'vue'
import { CACHE_INCLUDED_ROUTES } from "@/store/mutation-types" import { CACHE_INCLUDED_ROUTES } from "@/store/mutation-types"
import store from '../../store'
export default { export default {
name: 'TabLayout', name: 'TabLayout',
@@ -57,7 +65,8 @@
{ key: '2', icon: 'arrow-right', text: '关闭右侧' }, { key: '2', icon: 'arrow-right', text: '关闭右侧' },
{ key: '3', icon: 'close', text: '关闭其它' } { key: '3', icon: 'close', text: '关闭其它' }
], ],
reloadFlag:true reloadFlag:true,
componentsArr: []
} }
}, },
provide(){ provide(){
@@ -66,6 +75,10 @@
} }
}, },
computed: { computed: {
// 实现懒加载只渲染已经打开过hasOpen:true的iframe页
hasOpenComponentsArr() {
return this.componentsArr.filter(item => item.hasOpen)
},
multipage() { multipage() {
//判断如果是手机模式,自动切换为单页面模式 //判断如果是手机模式,自动切换为单页面模式
if (this.isMobile()) { if (this.isMobile()) {
@@ -76,15 +89,12 @@
}, },
includedComponents() { includedComponents() {
const includedRouters = Vue.ls.get(CACHE_INCLUDED_ROUTES) const includedRouters = Vue.ls.get(CACHE_INCLUDED_ROUTES)
//console.log("includedRouters" + includedRouters)
//加入到 cache_included_routes //加入到 cache_included_routes
if (this.$route.meta.componentName) { if (this.$route.meta.componentName) {
let cacheRouterArray = Vue.ls.get(CACHE_INCLUDED_ROUTES) || [] let cacheRouterArray = Vue.ls.get(CACHE_INCLUDED_ROUTES) || []
if(!cacheRouterArray.includes(this.$route.meta.componentName)){ if(!cacheRouterArray.includes(this.$route.meta.componentName)){
cacheRouterArray.push(this.$route.meta.componentName) cacheRouterArray.push(this.$route.meta.componentName)
//console.log("Vue ls set componentName " + this.$route.meta.componentName)
Vue.ls.set(CACHE_INCLUDED_ROUTES, cacheRouterArray) Vue.ls.set(CACHE_INCLUDED_ROUTES, cacheRouterArray)
//console.log("Vue ls includedRouterArrays " + Vue.ls.get(CACHE_INCLUDED_ROUTES))
return cacheRouterArray; return cacheRouterArray;
} }
} }
@@ -92,6 +102,15 @@
} }
}, },
created() { created() {
// 设置iframe页的数组对象
const componentsArr = this.getComponentsArr()
componentsArr.forEach((item) => {
Vue.component(item.name, item.component)
})
this.componentsArr = componentsArr
// 判断当前路由是否iframe页
this.isOpenIframePage()
if (this.$route.path != indexKey) { if (this.$route.path != indexKey) {
this.addIndexToFirst() this.addIndexToFirst()
} }
@@ -144,8 +163,44 @@
this.addIndexToFirst() this.addIndexToFirst()
} }
}, },
$route() {
// 判断当前路由是否iframe页
this.isOpenIframePage()
}
}, },
methods: { methods: {
// 根据当前路由设置hasOpen
isOpenIframePage() {
const target = this.componentsArr.find(item => {
return item.path === this.$route.path
})
if (target && !target.hasOpen) {
target.hasOpen = true;
}
},
// 遍历路由的所有页面把含有iframeComponent标识的收集起来
getComponentsArr() {
let iframeArr = []
const routers = this.$store.state.permission.routers;
for (let i = 0; i < routers.length; i++) {
if(routers[i].children) {
for (let j = 0; j < routers[i].children.length; j++) {
if(routers[i].children[j].iframeComponent) {
iframeArr.push(routers[i].children[j])
}
}
}
}
return iframeArr.map((item) => {
const name = item.name || item.path.replace('/', '')
return {
name: name,
path: item.path,
hasOpen: false, // 是否打开过默认false
component: item.iframeComponent // 组件文件的引用
}
})
},
// 将首页添加到第一位 // 将首页添加到第一位
addIndexToFirst() { addIndexToFirst() {
this.pageList.splice(0, 0, { this.pageList.splice(0, 0, {
@@ -208,6 +263,12 @@
Vue.ls.set(CACHE_INCLUDED_ROUTES, cacheRouterArray) Vue.ls.set(CACHE_INCLUDED_ROUTES, cacheRouterArray)
} }
} }
//从iframe缓存中关闭对应的页面
this.componentsArr.find(item => {
if(item.path === key) {
item.hasOpen = false
}
})
//update-end--Author:scott Date:20201015 for路由缓存问题关闭了tab页时再打开就不刷新 #842 //update-end--Author:scott Date:20201015 for路由缓存问题关闭了tab页时再打开就不刷新 #842
}, },
onContextmenu(e) { onContextmenu(e) {

View File

@@ -123,7 +123,6 @@ function generateChildRouters (data) {
let menu = { let menu = {
path: item.url, path: item.url,
name: item.text, name: item.text,
component: componentPath,
meta: { meta: {
id: item.id, id: item.id,
title: item.text, title: item.text,
@@ -132,9 +131,14 @@ function generateChildRouters (data) {
componentName:componentName, componentName:componentName,
internalOrExternal:true, internalOrExternal:true,
keepAlive: true keepAlive: true
// permissionList:""
} }
} }
if(item.component.indexOf("IframePageView")>-1){
//给带iframe的页面进行改造
menu.iframeComponent = componentPath
} else {
menu.component = componentPath
}
if (item.children && item.children.length > 0) { if (item.children && item.children.length > 0) {
menu.children = [...generateChildRouters( item.children)]; menu.children = [...generateChildRouters( item.children)];
} }