這篇文章主要介紹Vue中對iframe實現keep alive無刷新的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Vue的keep-alive原理
要實現對保持iframe頁的狀態。我們先搞清楚為什么Vue的keep-alive不能湊效。keep-alive原理是把組件里的節點信息保留在了 VNode (在內存里),在需要渲染時候從Vnode渲染到真實DOM上。iframe頁里的內容并不屬于節點的信息,所以使用keep-alive依然會重新渲染iframe內的內容。 另外 ,我也嘗試有過想法:如果把整個iframe節點保存起來,然后需要切換時把它渲染到目標節點上,能否實現iframe頁不被刷新呢?————也是不可行的,iframe每一次渲染就相當于打開一個新的網頁窗口,即使把節點保存下來,在渲染時iframe頁還是刷新的。
實現的思路
既然保持iframe頁里的狀態很難實現,在這個時候我想到了一個別的方法。能否在Vue的route-view節點上動點手腳?使得在切換 非iframe頁 的時候使用Vue的路由,當切換 iframe頁 時則使用 v-show 切換顯示與隱藏,使得iframe節點 一直不被刪除 ,這樣就能保持iframe的狀態了。
我們簡陋的實現一下以上的效果,上代碼:
入口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>上面代碼簡單來說,關鍵的地方首先是main.js初始化路由時,對iframe頁不填寫屬性component,這樣頁面就是空白的。然后在 router-view 節點旁邊渲染iframe頁組件,使用$route.path判斷當前路由的指向,控制iframe頁的 顯示與隱藏 。
上面代碼簡單的解決了問題,但還有一些地方可以優化:
iframe頁在根節點App.vue一渲染時 已經渲染 了,對此iframe頁可以做成 懶加載 ,只有在進入過相應頁面了觸發渲染,并且渲染過之后就用v-show切換顯示與隱藏
每當增加一個iframe頁都要增加一段的組件引入注冊和調用的代碼。比較 繁瑣 。我們目標應該做到每增加一個iframe頁,只需要添加盡量少的代碼。這里思路是:
在路由配置中定義一個屬性,用于 標識該頁面是否含有iframe 的頁面
根據標識,iframe頁組件 自動動態注冊和渲染 ,無需再手寫額外的代碼
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')接下來我們第二步和第三步結合在一起,封裝新的組件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>該組件主要做的是根據main.ja里的routes生成一個只含有iframe頁的數組對象。
watch上監聽$route,判斷當前頁面在iframe頁列表里的話就設置hasOpen屬性為true,渲染該組件
用v-show="$route.path === item.path"切換iframe頁的顯示與隱藏。
以上是“Vue中對iframe實現keep alive無刷新的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。