Vue中對iframe實現(xiàn)keep alive無刷新的方法
前言
最近一個需求,需要在 Vue 項目中加入 含有iframe 的頁面,同時在路由切換的過程中,要求iframe的內(nèi)容 不會被刷新 。一開始使用了Vue自帶的keep- alive發(fā)現(xiàn)沒有用,于是自己研究了一下解決方案。。。。。。
Vue的keep-alive原理
要實現(xiàn)對保持iframe頁的狀態(tài)。我們先搞清楚為什么Vue的keep-alive不能湊效。keep-alive原理是把組件里的節(jié)點信息保留在了 VNode (在內(nèi)存里),在需要渲染時候從Vnode渲染到真實DOM上。iframe頁里的內(nèi)容并不屬于節(jié)點的信息,所以使用keep-alive依然會重新渲染iframe內(nèi)的內(nèi)容。 另外 ,我也嘗試有過想法:如果把整個iframe節(jié)點保存起來,然后需要切換時把它渲染到目標節(jié)點上,能否實現(xiàn)iframe頁不被刷新呢?————也是不可行的,iframe每一次渲染就相當于打開一個新的網(wǎng)頁窗口,即使把節(jié)點保存下來,在渲染時iframe頁還是刷新的。
實現(xiàn)的思路
既然保持iframe頁里的狀態(tài)很難實現(xiàn),在這個時候我想到了一個別的方法。能否在Vue的route-view節(jié)點上動點手腳?使得在切換 非iframe頁 的時候使用Vue的路由,當切換 iframe頁 時則使用 v-show 切換顯示與隱藏,使得iframe節(jié)點 一直不被刪除 ,這樣就能保持iframe的狀態(tài)了。
我們簡陋的實現(xiàn)一下以上的效果,上代碼:
入口main.js:
import Vue from 'vue/dist/vue.js'
import App from './App.vue'
import VueRouter from 'vue-router';
const Index = { template: '<div>Index</div>' }
const routes = [
// 含有iframe的兩個頁面
{
path: '/f1',
name: 'f1'
},
// 含有iframe的兩個頁面
{
path: '/f2',
name: 'f2'
},
{
path: '/index',
component: Index
}
]
const router = new VueRouter({
routes
});
Vue.use(VueRouter);
new Vue({
render: h => h(App),
router
}).$mount('#app')
根組件:
<template>
<div id="app">
<div class="nav">
<router-link class="router" to="/f1">Go to F1</router-link>
<router-link class="router" to="/f2">Go to F2</router-link>
<router-link class="router" to="/index">Go to Index</router-link>
</div>
<keep-alive>
<!-- Vue的路由 -->
<router-view></router-view>
</keep-alive>
<!-- iframe頁面 -->
<f1 v-show="$route.path == '/f1'"></f1>
<f2 v-show="$route.path == '/f2'"></f2>
</div>
</template>
<script>
import F1 from './components/f1';
import F2 from './components/f2';
export default {
name: 'app',
components: {
F1,
F2
},
}
</script>
上面代碼簡單來說,關(guān)鍵的地方首先是main.js初始化路由時,對iframe頁不填寫屬性component,這樣頁面就是空白的。然后在 router-view 節(jié)點旁邊渲染iframe頁組件,使用$route.path判斷當前路由的指向,控制iframe頁的 顯示與隱藏 。
上面代碼簡單的解決了問題,但還有一些地方可以優(yōu)化:
- iframe頁在根節(jié)點App.vue一渲染時 已經(jīng)渲染 了,對此iframe頁可以做成 懶加載 ,只有在進入過相應(yīng)頁面了觸發(fā)渲染,并且渲染過之后就用v-show切換顯示與隱藏
- 每當增加一個iframe頁都要增加一段的組件引入注冊和調(diào)用的代碼。比較 繁瑣 。我們目標應(yīng)該做到每增加一個iframe頁,只需要添加盡量少的代碼。這里思路是:
- 在路由配置中定義一個屬性,用于 標識該頁面是否含有iframe 的頁面
- 根據(jù)標識,iframe頁組件 自動動態(tài)注冊和渲染 ,無需再手寫額外的代碼
- router-view和iframe切換的邏輯封裝成 新組件 ,用它 替代原有的router-view
我們先修改router的配置,增加一個屬性名iframeComponent,用于標識是否包含iframe,該屬性的值是組件文件引用
main.js:
import F1 from './components/f1';
import F2 from './components/f2';
const routes = [
{
path: '/f1',
name: 'f1',
iframeComponent: F1 // 用于標識是否含有iframe頁
},
{
path: '/f2',
name: 'f2',
iframeComponent: F2 // 用于標識是否含有iframe頁
},
{
path: '/index',
component: { template: '<div>Index</div>' }
}
]
const router = new VueRouter({
routes // (縮寫)相當于 routes: routes
});
new Vue({
render: h => h(App),
router
}).$mount('#app')
接下來我們第二步和第三步結(jié)合在一起,封裝新的組件iframe-router-view.vue:
<template>
<div>
<!-- Vue的router-view -->
<keep-alive>
<router-view></router-view>
</keep-alive>
<!-- iframe頁 -->
<component
v-for="item in hasOpenComponentsArr"
:key="item.name"
:is="item.name"
v-show="$route.path === item.path"
></component>
</div>
</template>
<script>
import Vue from 'vue/dist/vue.js'
export default {
created() {
// 設(shè)置iframe頁的數(shù)組對象
const componentsArr = this.getComponentsArr();
componentsArr.forEach((item) => {
Vue.component(item.name, item.component);
});
this.componentsArr = componentsArr;
// 判斷當前路由是否iframe頁
this.isOpenIframePage();
},
data() {
return {
componentsArr: [] // 含有iframe的頁面
}
},
watch: {
$route() {
// 判斷當前路由是否iframe頁
this.isOpenIframePage();
}
},
computed: {
// 實現(xiàn)懶加載,只渲染已經(jīng)打開過(hasOpen:true)的iframe頁
hasOpenComponentsArr() {
return this.componentsArr.filter(item => item.hasOpen);
}
},
methods: {
// 根據(jù)當前路由設(shè)置hasOpen
isOpenIframePage() {
const target = this.componentsArr.find(item => {
return item.path === this.$route.path
});
if (target && !target.hasOpen) {
target.hasOpen = true;
}
},
// 遍歷路由的所有頁面,把含有iframeComponent標識的收集起來
getComponentsArr() {
const router = this.$router;
const routes = router.options.routes;
const iframeArr = routes.filter(item => item.iframeComponent);
return iframeArr.map((item) => {
const name = item.name || item.path.replace('/', '');
return {
name: name,
path: item.path,
hasOpen: false, // 是否打開過,默認false
component: item.iframeComponent // 組件文件的引用
};
});
}
}
}
</script>
- 該組件主要做的是根據(jù)main.ja里的routes生成一個只含有iframe頁的數(shù)組對象。
- watch上監(jiān)聽$route,判斷當前頁面在iframe頁列表里的話就設(shè)置hasOpen屬性為true,渲染該組件
- 用v-show="$route.path === item.path"切換iframe頁的顯示與隱藏。
邏輯并不復雜,這里就不多贅述。
結(jié)語
大家如果有更好的實現(xiàn)方法,或者我上面還有什么需要更正的錯誤,歡迎交流。 上面demo的代碼放在了個人github上 https://github.com/jmx164491960/vue-iframe-demo
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄
這篇文章主要介紹了vue中v-for循環(huán)數(shù)組,在方法中splice刪除數(shù)組元素踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
利用Vue3和Plotly.js創(chuàng)建交互式表格
在數(shù)據(jù)分析和可視化領(lǐng)域,經(jīng)常需要以表格的形式展示數(shù)據(jù),Plotly.js 是一款功能強大的 JavaScript 庫,不僅可以創(chuàng)建交互式圖表,還可以動態(tài)生成 HTML 表格,本文給大家介紹了如何用Vue3和Plotly.js創(chuàng)建交互式表格,需要的朋友可以參考下2024-07-07
vue中為什么在組件內(nèi)部data是一個函數(shù)而不是一個對象
這篇文章主要介紹了vue中為什么在組件內(nèi)部data是一個函數(shù)而不是一個對象,本文通過示例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue2.x,vue3.x使用provide/inject注入的區(qū)別說明
這篇文章主要介紹了vue2.x,vue3.x使用provide/inject注入的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

