国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue中對iframe實(shí)現(xiàn)keep alive無刷新的方法

2019-11-19 11:09:10
字體:
供稿:網(wǎng)友

前言

最近一個(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)化:

  1. iframe頁在根節(jié)點(diǎn)App.vue一渲染時(shí) 已經(jīng)渲染 了,對此iframe頁可以做成 懶加載 ,只有在進(jìn)入過相應(yīng)頁面了觸發(fā)渲染,并且渲染過之后就用v-show切換顯示與隱藏
  2. 每當(dāng)增加一個(gè)iframe頁都要增加一段的組件引入注冊和調(diào)用的代碼。比較 繁瑣 。我們目標(biāo)應(yīng)該做到每增加一個(gè)iframe頁,只需要添加盡量少的代碼。這里思路是:
    1. 在路由配置中定義一個(gè)屬性,用于 標(biāo)識該頁面是否含有iframe 的頁面
    2. 根據(jù)標(biāo)識,iframe頁組件 自動動態(tài)注冊和渲染 ,無需再手寫額外的代碼
    3. router-view和iframe切換的邏輯封裝成 新組件 ,用它 替代原有的router-view

我們先修改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>
  1. 該組件主要做的是根據(jù)main.ja里的routes生成一個(gè)只含有iframe頁的數(shù)組對象。
  2. watch上監(jiān)聽$route,判斷當(dāng)前頁面在iframe頁列表里的話就設(shè)置hasOpen屬性為true,渲染該組件
  3. 用v-show="$route.path === item.path"切換iframe頁的顯示與隱藏。

邏輯并不復(fù)雜,這里就不多贅述。

結(jié)語

大家如果有更好的實(shí)現(xiàn)方法,或者我上面還有什么需要更正的錯(cuò)誤,歡迎交流。 上面demo的代碼放在了個(gè)人github上 https://github.com/jmx164491960/vue-iframe-demo

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桃源县| 德兴市| 新宁县| 福鼎市| 蛟河市| 灵寿县| 屯门区| 台北市| 凌源市| 喀喇沁旗| 郸城县| 乐东| 湄潭县| 晋江市| 南和县| 章丘市| 东兰县| 龙南县| 邳州市| 金乡县| 伊宁县| 吉木萨尔县| 万源市| 松桃| 扎兰屯市| 喀什市| 额敏县| 包头市| 彝良县| 广东省| 共和县| 霞浦县| 昌黎县| 恭城| 调兵山市| 瑞安市| 濉溪县| 云梦县| 崇州市| 漠河县| 泗阳县|