溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue中怎么對iframe實現(xiàn)keep alive無刷新

發(fā)布時間:2022-04-28 16:50:55 來源:億速云 閱讀:908 作者:iii 欄目:大數(shù)據(jù)

本文小編為大家詳細介紹“Vue中怎么對iframe實現(xiàn)keep alive無刷新”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“Vue中怎么對iframe實現(xiàn)keep alive無刷新”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

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每一次渲染就相當(dāng)于打開一個新的網(wǎng)頁窗口,即使把節(jié)點保存下來,在渲染時iframe頁還是刷新的。

實現(xiàn)的思路

既然保持iframe頁里的狀態(tài)很難實現(xiàn),在這個時候我想到了一個別的方法。能否在Vue的route-view節(jié)點上動點手腳?使得在切換 非iframe頁 的時候使用Vue的路由,當(dāng)切換 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判斷當(dāng)前路由的指向,控制iframe頁的 顯示與隱藏 。

上面代碼簡單的解決了問題,但還有一些地方可以優(yōu)化:

  1. iframe頁在根節(jié)點App.vue一渲染時 已經(jīng)渲染 了,對此iframe頁可以做成 懶加載 ,只有在進入過相應(yīng)頁面了觸發(fā)渲染,并且渲染過之后就用v-show切換顯示與隱藏

  2. 每當(dāng)增加一個iframe頁都要增加一段的組件引入注冊和調(diào)用的代碼。比較 繁瑣 。我們目標應(yīng)該做到每增加一個iframe頁,只需要添加盡量少的代碼。這里思路是:

    1. 在路由配置中定義一個屬性,用于 標識該頁面是否含有iframe 的頁面

    2. 根據(jù)標識,iframe頁組件 自動動態(tài)注冊和渲染 ,無需再手寫額外的代碼

    3. 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 // (縮寫)相當(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: {
  // 實現(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標識的收集起來
  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>
  1. 該組件主要做的是根據(jù)main.ja里的routes生成一個只含有iframe頁的數(shù)組對象。

  2. watch上監(jiān)聽$route,判斷當(dāng)前頁面在iframe頁列表里的話就設(shè)置hasOpen屬性為true,渲染該組件

  3. 用v-show="$route.path === item.path"切換iframe頁的顯示與隱藏。

讀到這里,這篇“Vue中怎么對iframe實現(xiàn)keep alive無刷新”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI