Vue中對iframe實現keep alive(無刷新)

前言

最近一個需求,須要在Vue項目中加入含有iframe的頁面,同時在路由切換的過程當中,要求iframe的內容不會被刷新。一開始使用了Vue自帶的keep- alive發現沒有用,因而本身研究了一下解決方案。。。。。。vue

Vue的keep-alive原理

要實現對保持iframe頁的狀態。咱們先搞清楚爲何Vue的keep-alive不能湊效。keep-alive原理是把組件裏的節點信息保留在了VNode(在內存裏),在須要渲染時候從Vnode渲染到真實DOM上。iframe頁裏的內容並不屬於節點的信息,因此使用keep-alive依然會從新渲染iframe內的內容。另外,我也嘗試有過想法:若是把整個iframe節點保存起來,而後須要切換時把它渲染到目標節點上,可否實現iframe頁不被刷新呢?————也是不可行的,iframe每一次渲染就至關於打開一個新的網頁窗口,即便把節點保存下來,在渲染時iframe頁仍是刷新的。node

實現的思路

既然保持iframe頁裏的狀態很難實現,在這個時候我想到了一個別的方法。可否在Vue的route-view節點上動點手腳?使得在切換非iframe頁的時候使用Vue的路由,當切換iframe頁時則使用v-show切換顯示與隱藏,使得iframe節點一直不被刪除,這樣就能保持iframe的狀態了。git

咱們簡陋的實現一下以上的效果,上代碼:github

入口main.js:vue-router

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>
複製代碼

上面代碼簡單來講,關鍵的地方首先是main.js初始化路由時,對iframe頁不填寫屬性component,這樣頁面就是空白的。而後在router-view節點旁邊渲染iframe頁組件,使用$route.path判斷當前路由的指向,控制iframe頁的顯示與隱藏bash

優化

上面代碼簡單的解決了問題,但還有一些地方能夠優化:app

  1. iframe頁在根節點App.vue一渲染時已經渲染了,對此iframe頁能夠作成懶加載,只有在進入過相應頁面了觸發渲染,而且渲染過以後就用v-show切換顯示與隱藏
  2. 每當增長一個iframe頁都要增長一段的組件引入註冊和調用的代碼。比較繁瑣。咱們目標應該作到每增長一個iframe頁,只須要添加儘可能少的代碼。這裏思路是:
    1. 在路由配置中定義一個屬性,用於標識該頁面是否含有iframe的頁面
    2. 根據標識,iframe頁組件自動動態註冊和渲染,無需再手寫額外的代碼
    3. router-view和iframe切換的邏輯封裝成新組件,用它替代原有的router-view

咱們先修改router的配置,增長一個屬性名iframeComponent,用於標識是否包含iframe,該屬性的值是組件文件引用優化

main.js:ui

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')

複製代碼

接下來咱們第二步和第三步結合在一塊兒,封裝新的組件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() {
        // 設置iframe頁的數組對象
        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: {
        // 實現懶加載,只渲染已經打開過(hasOpen:true)的iframe頁
        hasOpenComponentsArr() {
            return this.componentsArr.filter(item => item.hasOpen);
        }
    },
    methods: {
        // 根據當前路由設置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. 該組件主要作的是根據main.ja裏的routes生成一個只含有iframe頁的數組對象。
  2. watch上監聽$route,判斷當前頁面在iframe頁列表裏的話就設置hasOpen屬性爲true,渲染該組件
  3. 用v-show="$route.path === item.path"切換iframe頁的顯示與隱藏。

邏輯並不複雜,這裏就很少贅述。

結語

你們若是有更好的實現方法,或者我上面還有什麼須要更正的錯誤,歡迎交流。 上面demo的代碼放在了我的github上github.com/jmx16449196…

相關文章
相關標籤/搜索