前言
最近一個(gè)需求,需要在 Vue 項(xiàng)目中加入 含有iframe 的頁面,同時(shí)在路由切換的過程中,要求iframe的內(nèi)容 不會被刷新 。一開始使用了Vue自帶的keep- alive發(fā)現(xiàn)沒有用,于是自己研究了一下解決方案。。。。。。
Vue的keep-alive原理
要實(shí)現(xiàn)對保持iframe頁的狀態(tài)。我們先搞清楚為什么Vue的keep-alive不能湊效。keep-alive原理是把組件里的節(jié)點(diǎn)信息保留在了 VNode (在內(nèi)存里),在需要渲染時(shí)候從Vnode渲染到真實(shí)DOM上。iframe頁里的內(nèi)容并不屬于節(jié)點(diǎn)的信息,所以使用keep-alive依然會重新渲染iframe內(nèi)的內(nèi)容。 另外 ,我也嘗試有過想法:如果把整個(gè)iframe節(jié)點(diǎn)保存起來,然后需要切換時(shí)把它渲染到目標(biāo)節(jié)點(diǎn)上,能否實(shí)現(xiàn)iframe頁不被刷新呢?――――也是不可行的,iframe每一次渲染就相當(dāng)于打開一個(gè)新的網(wǎng)頁窗口,即使把節(jié)點(diǎn)保存下來,在渲染時(shí)iframe頁還是刷新的。
實(shí)現(xiàn)的思路
既然保持iframe頁里的狀態(tài)很難實(shí)現(xiàn),在這個(gè)時(shí)候我想到了一個(gè)別的方法。能否在Vue的route-view節(jié)點(diǎn)上動點(diǎn)手腳?使得在切換 非iframe頁 的時(shí)候使用Vue的路由,當(dāng)切換 iframe頁 時(shí)則使用 v-show 切換顯示與隱藏,使得iframe節(jié)點(diǎn) 一直不被刪除 ,這樣就能保持iframe的狀態(tài)了。
我們簡陋的實(shí)現(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的兩個(gè)頁面 { path: '/f1', name: 'f1' }, // 含有iframe的兩個(gè)頁面 { 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初始化路由時(shí),對iframe頁不填寫屬性component,這樣頁面就是空白的。然后在 router-view 節(jié)點(diǎn)旁邊渲染iframe頁組件,使用$route.path判斷當(dāng)前路由的指向,控制iframe頁的 顯示與隱藏 。
上面代碼簡單的解決了問題,但還有一些地方可以優(yōu)化:
我們先修改router的配置,增加一個(gè)屬性名iframeComponent,用于標(biāo)識是否包含iframe,該屬性的值是組件文件引用
main.js:
import F1 from './components/f1';import F2 from './components/f2';const routes = [ { path: '/f1', name: 'f1', iframeComponent: F1 // 用于標(biāo)識是否含有iframe頁 }, { path: '/f2', name: 'f2', iframeComponent: F2 // 用于標(biāo)識是否含有iframe頁 }, { path: '/index', component: { template: '<div>Index</div>' } }]const router = new VueRouter({ routes // (縮寫)相當(dāng)于 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; // 判斷當(dāng)前路由是否iframe頁 this.isOpenIframePage(); }, data() { return { componentsArr: [] // 含有iframe的頁面 } }, watch: { $route() { // 判斷當(dāng)前路由是否iframe頁 this.isOpenIframePage(); } }, computed: { // 實(shí)現(xiàn)懶加載,只渲染已經(jīng)打開過(hasOpen:true)的iframe頁 hasOpenComponentsArr() { return this.componentsArr.filter(item => item.hasOpen); } }, methods: { // 根據(jù)當(dāng)前路由設(shè)置hasOpen isOpenIframePage() { const target = this.componentsArr.find(item => { return item.path === this.$route.path }); if (target && !target.hasOpen) { target.hasOpen = true; } }, // 遍歷路由的所有頁面,把含有iframeComponent標(biāo)識的收集起來 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, // 是否打開過,默認(rèn)false component: item.iframeComponent // 組件文件的引用 }; }); } }}</script>邏輯并不復(fù)雜,這里就不多贅述。
結(jié)語
大家如果有更好的實(shí)現(xiàn)方法,或者我上面還有什么需要更正的錯(cuò)誤,歡迎交流。 上面demo的代碼放在了個(gè)人github上 https://github.com/jmx164491960/vue-iframe-demo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答