亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue.js內置組件之keep-alive組件使用

 更新時間:2018年07月10日 16:20:24   作者:walker-design  
keep-alive是Vue.js的一個內置組件。這篇文章主要介紹了vue.js內置組件之keep-alive組件使用,需要的朋友可以參考下

keep-alive是Vue.js的一個內置組件。<keep-alive> 包裹動態(tài)組件時,會緩存不活動的組件實例,而不是銷毀它們。它自身不會渲染一個 DOM 元素,也不會出現(xiàn)在父組件鏈中。 當組件在 <keep-alive> 內被切換,它的 activated 和 deactivated 這兩個生命周期鉤子函數將會被對應執(zhí)行。 它提供了include與exclude兩個屬性,允許組件有條件地進行緩存。

舉個栗子

<keep-alive>
  <router-view v-if="$route.meta.keepAlive"></router-view>
 </keep-alive>
 <router-view v-if="!$route.meta.keepAlive"></router-view>

在點擊button時候,兩個input會發(fā)生切換,但是這時候這兩個輸入框的狀態(tài)會被緩存起來,input標簽中的內容不會因為組件的切換而消失。

* include - 字符串或正則表達式。只有匹配的組件會被緩存。
* exclude - 字符串或正則表達式。任何匹配的組件都不會被緩存。

<keep-alive include="a">
 <component></component>
</keep-alive>

只緩存組件別民name為a的組件

<keep-alive exclude="a">
 <component></component>
</keep-alive>

除了name為a的組件,其他都緩存下來

生命周期鉤子

生命鉤子 keep-alive提供了兩個生命鉤子,分別是activated與deactivated。

因為keep-alive會將組件保存在內存中,并不會銷毀以及重新創(chuàng)建,所以不會重新調用組件的created等方法,需要用activated與deactivated這兩個生命鉤子來得知當前組件是否處于活動狀態(tài)。

深入keep-alive組件實現(xiàn)

 

查看vue--keep-alive組件源代碼可以得到以下信息

created鉤子會創(chuàng)建一個cache對象,用來作為緩存容器,保存vnode節(jié)點。

props: {
 include: patternTypes,
 exclude: patternTypes,
 max: [String, Number]
},
created () {
 // 創(chuàng)建緩存對象
 this.cache = Object.create(null)
 // 創(chuàng)建一個key別名數組(組件name)
 this.keys = []
},

destroyed鉤子則在組件被銷毀的時候清除cache緩存中的所有組件實例。

destroyed () {
 /* 遍歷銷毀所有緩存的組件實例*/
 for (const key in this.cache) {
  pruneCacheEntry(this.cache, key, this.keys)
 }
},

:::demo

render () {
 /* 獲取插槽 */
 const slot = this.$slots.default
 /* 根據插槽獲取第一個組件組件 */
 const vnode: VNode = getFirstComponentChild(slot)
 const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
 if (componentOptions) {
 // 獲取組件的名稱(是否設置了組件名稱name,沒有則返回組件標簽名稱)
 const name: ?string = getComponentName(componentOptions)
 // 解構對象賦值常量
 const { include, exclude } = this
 if ( /* name不在inlcude中或者在exlude中則直接返回vnode */
  // not included
  (include && (!name || !matches(include, name))) ||
  // excluded
  (exclude && name && matches(exclude, name))
 ) {
  return vnode
 }
 const { cache, keys } = this
 const key: ?string = vnode.key == null
  // same constructor may get registered as different local components
  // so cid alone is not enough (#3269)
  ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
  : vnode.key
 if (cache[key]) { // 判斷當前是否有緩存,有則取緩存的實例,無則進行緩存
  vnode.componentInstance = cache[key].componentInstance
  // make current key freshest
  remove(keys, key)
  keys.push(key)
 } else {
  cache[key] = vnode
  keys.push(key)
  // 判斷是否設置了最大緩存實例數量,超過則刪除最老的數據,
  if (this.max && keys.length > parseInt(this.max)) {
  pruneCacheEntry(cache, keys[0], keys, this._vnode)
  }
 }
 // 給vnode打上緩存標記
 vnode.data.keepAlive = true
 }
 return vnode || (slot && slot[0])
}
// 銷毀實例
function pruneCacheEntry (
 cache: VNodeCache,
 key: string,
 keys: Array<string>,
 current?: VNode
) {
 const cached = cache[key]
 if (cached && (!current || cached.tag !== current.tag)) {
 cached.componentInstance.$destroy()
 }
 cache[key] = null
 remove(keys, key)
}
// 緩存
function pruneCache (keepAliveInstance: any, filter: Function) {
 const { cache, keys, _vnode } = keepAliveInstance
 for (const key in cache) {
 const cachedNode: ?VNode = cache[key]
 if (cachedNode) {
  const name: ?string = getComponentName(cachedNode.componentOptions)
  // 組件name 不符合filler條件, 銷毀實例,移除cahe
  if (name && !filter(name)) {
  pruneCacheEntry(cache, key, keys, _vnode)
  }
 }
 }
}
// 篩選過濾函數
function matches (pattern: string | RegExp | Array<string>, name: string): boolean {
 if (Array.isArray(pattern)) {
 return pattern.indexOf(name) > -1
 } else if (typeof pattern === 'string') {
 return pattern.split(',').indexOf(name) > -1
 } else if (isRegExp(pattern)) {
 return pattern.test(name)
 }
 /* istanbul ignore next */
 return false
}
// 檢測 include 和 exclude 數據的變化,實時寫入讀取緩存或者刪除
mounted () {
 this.$watch('include', val => {
 pruneCache(this, name => matches(val, name))
 })
 this.$watch('exclude', val => {
 pruneCache(this, name => !matches(val, name))
 })
},

:::

通過查看Vue源碼可以看出,keep-alive默認傳遞3個屬性,include 、exclude、max, max 最大可緩存的長度

結合源碼我們可以實現(xiàn)一個可配置緩存的router-view

<!--exclude - 字符串或正則表達式。任何匹配的組件都不會被緩存。-->
<!--TODO 匹配首先檢查組件自身的 name 選項,如果 name 選項不可用,則匹配它的局部注冊名稱-->
<keep-alive :exclude="keepAliveConf.value">
 <router-view class="child-view" :key="$route.fullPath"></router-view>
</keep-alive>
<!-- 或者 -->
<keep-alive :include="keepAliveConf.value">
 <router-view class="child-view" :key="$route.fullPath"></router-view>
</keep-alive>
<!-- 具體使用 include 還是exclude 根據項目是否需要緩存的頁面數量多少來決定-->

創(chuàng)建一個keepAliveConf.js 放置需要匹配的組件名

// 路由組件命名集合
 var arr = ['component1', 'component2'];
 export default {value: routeList.join()};

配置重置緩存的全局方法

import keepAliveConf from 'keepAliveConf.js'
Vue.mixin({
 methods: {
 // 傳入需要重置的組件名字
 resetKeepAive(name) {
  const conf = keepAliveConf.value;
  let arr = keepAliveConf.value.split(',');
  if (name && typeof name === 'string') {
   let i = arr.indexOf(name);
   if (i > -1) {
    arr.splice(i, 1);
    keepAliveConf.value = arr.join();
    setTimeout(() => {
     keepAliveConf.value = conf
    }, 500);
   }
  }
 },
 }
})

在合適的時機調用調用this.resetKeepAive(name),觸發(fā)keep-alive銷毀組件實例;

Vue.js內部將DOM節(jié)點抽象成了一個個的VNode節(jié)點,keep-alive組件的緩存也是基于VNode節(jié)點的而不是直接存儲DOM結構。它將滿足條件的組件在cache對象中緩存起來,在需要重新渲染的時候再將vnode節(jié)點從cache對象中取出并渲染。

總結

以上所述是小編給大家介紹的vue.js內置組件之keep-alive組件使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

  • 關于Vue父子組件傳參和回調函數的使用

    關于Vue父子組件傳參和回調函數的使用

    這篇文章主要介紹了關于Vue父子組件傳參和回調函數的使用,我們將某段代碼封裝成一個組件,而這個組件又在另一個組件中引入,而引入該封裝的組件的文件叫做父組件,被引入的組件叫做子組件,需要的朋友可以參考下
    2023-05-05
  • Vue3的vue-router超詳細使用示例教程

    Vue3的vue-router超詳細使用示例教程

    這篇文章主要介紹了Vue3的vue-router超詳細使用,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • vue圖片拖拉轉放大縮小組件使用詳解

    vue圖片拖拉轉放大縮小組件使用詳解

    這篇文章主要為大家詳細介紹了vue圖片拖拉轉放大縮小組件的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • VUE中如何渲染Echarts動畫柱狀圖

    VUE中如何渲染Echarts動畫柱狀圖

    這篇文章主要介紹了VUE中如何渲染Echarts動畫柱狀圖問題,具有很好的參考價值,希望對大家有所幫助。
    2023-03-03
  • Vue3動態(tài)路由(響應式帶參數的路由)變更頁面不刷新的問題解決辦法

    Vue3動態(tài)路由(響應式帶參數的路由)變更頁面不刷新的問題解決辦法

    問題來源是因為我的開源項目Maple-Boot項目的網站前端,因為項目主打的內容發(fā)布展示,所以其中的內容列表頁會根據不同的菜單進行渲染不同的路由,本文降介紹Vue3動態(tài)路由變更頁面不刷新的問題解決辦法,需要的朋友可以參考下
    2024-07-07
  • Vue收集依賴與觸發(fā)依賴源碼刨析

    Vue收集依賴與觸發(fā)依賴源碼刨析

    vue對依賴的管理使用的是發(fā)布訂閱者模式,其中watcher扮演訂閱者,Dep扮演發(fā)布者。所以dep中會有多個watcher,一個訂閱者也可以有多個發(fā)布者(依賴)??偣踩齻€過程:定義依賴、收集依賴、觸發(fā)依賴。下面開始詳細講解三個過程
    2022-10-10
  • vue 組件prop驗證作用示例解析

    vue 組件prop驗證作用示例解析

    這篇文章主要為大家介紹了vue組件prop驗證作用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • el-select單選時選擇后輸入框的is-focus狀態(tài)并沒有取消問題解決

    el-select單選時選擇后輸入框的is-focus狀態(tài)并沒有取消問題解決

    這篇文章主要給大家介紹了關于el-select單選時選擇后輸入框的is-focus狀態(tài)并沒有取消問題的解決過程,文中通過圖文以及代碼示例將解決的辦法介紹的非常詳細,需要的朋友可以參考下
    2024-01-01
  • van-picker組件default-index屬性設置不生效踩坑及解決

    van-picker組件default-index屬性設置不生效踩坑及解決

    這篇文章主要介紹了van-picker組件default-index屬性設置不生效踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 實用的 vue tags 創(chuàng)建緩存導航的過程實現(xiàn)

    實用的 vue tags 創(chuàng)建緩存導航的過程實現(xiàn)

    這篇文章主要介紹了實用的 vue tags 創(chuàng)建緩存導航的過程實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12

最新評論