最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue--keep-alive使用實例詳解

 更新時間:2022年08月24日 09:09:55   作者:IT利刃出鞘  
這篇文章主要介紹了Vue--keep-alive使用實例詳解,keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染,用示例代碼介紹Vue的keep-alive的用法,需要的朋友可以參考下

簡介

說明

本文用示例介紹Vue的keep-alive的用法。

keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染。

官網(wǎng)網(wǎng)址

https://v2.cn.vuejs.org/v2/api/#keep-alive

相關(guān)網(wǎng)址

vue2中的keep-alive使用總結(jié)及注意事項

公共代碼

路由(router/index.js)

import Vue from 'vue'
import VueRouter from 'vue-router'
import Parent from '../components/Parent'
 
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    name: 'Parent',
    component: Parent
  }
 
]
 
const router = new VueRouter({
  routes
})
 
export default router

生命周期mixin(mixins/LifeCycle.js)

下邊的兩個頁面組件都要打印生命周期,所以我把它抽出作為mixin。

export default {
  computed: {
    name () {
      return this.$options.name
    }
  },
  created () {
    console.log('created ==> ' + this.name)
  },
  activated () {
    console.log('activated ==> ' + this.name)
  },
  deactivated () {
    console.log('deactivated ==> ' + this.name)
  },
  destroyed () {
    console.log('destroyed ==> ' + this.name)
  }
}

子頁面組件

components/CompA.vue

<template>
  <div class="outer">
    <div>
      這是CompA
    </div>
  </div>
</template>
 
<script>
import LifeCycle from '../mixins/LifeCycle'
 
export default {
  name: 'CompA',
  mixins: [LifeCycle]
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

components/CompB.vue

<template>
  <div class="outer">
    這是CompB
  </div>
</template>
 
<script>
import LifeCycle from '../mixins/LifeCycle'
 
export default {
  name: 'CompB',
  mixins: [LifeCycle]
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid blue;
  padding: 20px;
}
</style>

動態(tài)組件實例

不加keep-alive

代碼

components/Parent.vue

<template>
  <div class="outer">
    <div>
      這是Parent
    </div>
    <br>
    <button @click="useCompA">切換到CompA</button>
    |
    <button @click="useCompB">切換到CompB</button>
    <component :is="componentName"></component>
  </div>
</template>
 
<script>
import CompA from './CompA'
import CompB from './CompB'
 
export default {
  name: 'Parent',
  components: {
    CompA,
    CompB
  },
  data () {
    return {
      componentName: ''
    }
  },
  methods: {
    useCompA () {
      this.componentName = 'CompA'
    },
    useCompB () {
      this.componentName = 'CompB'
    }
  }
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

測試

訪問:http://localhost:8080/#/

可以發(fā)現(xiàn):進(jìn)入組件時調(diào)用created;離開組件時調(diào)用destroyed。

加keep-alive

代碼

component/Parent.vue

將動態(tài)組件用keep-alive標(biāo)簽包起來。

<template>
  <div class="outer">
    <div>
      這是Parent
    </div>
    <br>
    <button @click="useCompA">切換到CompA</button>
    |
    <button @click="useCompB">切換到CompB</button>
    <keep-alive>
      <component :is="componentName"></component>
    </keep-alive>
  </div>
</template>
 
<script>
import CompA from './CompA'
import CompB from './CompB'
 
export default {
  name: 'Parent',
  components: {
    CompA,
    CompB
  },
  data () {
    return {
      componentName: ''
    }
  },
  methods: {
    useCompA () {
      this.componentName = 'CompA'
    },
    useCompB () {
      this.componentName = 'CompB'
    }
  }
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

測試

訪問:http://localhost:8080/#/

可以發(fā)現(xiàn):進(jìn)入組件時調(diào)用created、activated;離開組件時調(diào)用deactivated(不調(diào)用destroyed)。

router-view實例

不加keep-alive

代碼

修改路由設(shè)置(router/index.js)

import Vue from 'vue'
import VueRouter from 'vue-router'
import Parent from '../components/Parent'
import CompA from '../components/CompA'
import CompB from '../components/CompB'
 
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    name: 'Parent',
    component: Parent
  },
  {
    path: '/compA',
    name: 'CompA',
    component: CompA
  },
  {
    path: '/compB',
    name: 'CompB',
    component: CompB
  }
]
 
const router = new VueRouter({
  routes
})
 
export default router

修改入口組件(components/Parent.vue)

<template>
  <div class="outer">
    <div>
      這是Parent
    </div>
    <br>
    <router-link :to="{name: 'CompA'}">跳轉(zhuǎn)到CompA</router-link> 
    |
    <router-link :to="{name: 'CompB'}">跳轉(zhuǎn)到CompB</router-link>
    <router-view></router-view>
  </div>
</template>
 
<script>
 
export default {
  name: 'Parent'
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

測試

訪問:http://localhost:8080/#/

可以發(fā)現(xiàn):進(jìn)入路由組件時調(diào)用created;離開路由組件時調(diào)用destroyed。

加keep-alive(加到App.vue里)

修改App.vue

將router-view標(biāo)簽包裹在keep-alive標(biāo)簽里。

<template>
  <div id="app">
    <div id="nav">
      <router-link to="/">Home</router-link>
      |
      <router-link to="/about">About</router-link>
    </div>
    <!-- 原來寫法 -->
    <!-- <router-view/> -->
    <keep-alive>
      <router-view/>
    </keep-alive>
  </div>
</template>
 
<style>
<!-- 省略 -->
</style>

測試

訪問:http://localhost:8080/#/

可以發(fā)現(xiàn):進(jìn)入組件時調(diào)用created、activated;離開組件時調(diào)用deactivated(不調(diào)用destroyed)。

加keep-alive(加到Parent.vue里)

說明

需要做如下兩步:

  • Parent.vue:將router-view包裹在keep-alive里邊
  • 將CompA和CompB的路由作為Parent的嵌套路由(Children)

如果只修改Parent.vue,將router-view包裹在keep-alive里邊,這樣是沒用的,跟沒有加keep-alive效果一樣。

代碼

修改components/Parent.vue

<template>
  <div class="outer">
    <div>
      這是Parent
    </div>
    <br>
    <router-link :to="{name: 'CompA'}">跳轉(zhuǎn)到CompA</router-link>
    |
    <router-link :to="{name: 'CompB'}">跳轉(zhuǎn)到CompB</router-link>
    <keep-alive>
      <router-view></router-view>
    </keep-alive>
  </div>
</template>
 
<script>
 
export default {
  name: 'Parent'
}
</script>
 
<style scoped>
.outer {
  margin: 20px;
  border: 2px solid red;
  padding: 20px;
}
</style>

修改路由(router/index.js)

import Vue from 'vue'
import VueRouter from 'vue-router'
import Parent from '../components/Parent'
import CompA from '../components/CompA'
import CompB from '../components/CompB'
 
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    name: 'Parent',
    component: Parent,
    children: [
      {
        path: '/compA',
        name: 'CompA',
        component: CompA
      },
      {
        path: '/compB',
        name: 'CompB',
        component: CompB
      }
    ]
  }
 
]
 
const router = new VueRouter({
  routes
})
 
export default router

測試

訪問:http://localhost:8080/#/

到此這篇關(guān)于Vue--keep-alive使用實例詳解的文章就介紹到這了,更多相關(guān)Vue--keep-alive使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3 component is 不顯示的問題及解決

    vue3 component is 不顯示的問題及解決

    這篇文章主要介紹了vue3 component is 不顯示的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue2從數(shù)據(jù)變化到視圖變化發(fā)布訂閱模式詳解

    vue2從數(shù)據(jù)變化到視圖變化發(fā)布訂閱模式詳解

    這篇文章主要為大家介紹了vue2從數(shù)據(jù)變化到視圖變化發(fā)布訂閱模式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • vue+element+springboot實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例

    vue+element+springboot實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例

    本文主要介紹了vue + element-ui + springboot 實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count)

    elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count)

    項目中遇到數(shù)據(jù)量大,查詢的字段多,但用戶主要使用的是最近的一些數(shù)據(jù),1萬條以后的數(shù)據(jù)一般不使用,這篇文章主要介紹了elementUI Pagination 分頁指定最大頁的問題及解決方法(page-count),需要的朋友可以參考下
    2024-08-08
  • Vue實現(xiàn)跑馬燈簡單效果

    Vue實現(xiàn)跑馬燈簡單效果

    這篇文章主要為大家詳細(xì)介紹了Vues實現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 在React和Vue中使用Mock.js模擬接口的實現(xiàn)方法

    在React和Vue中使用Mock.js模擬接口的實現(xiàn)方法

    本文將介紹如何在React和Vue項目中使用Mock.js來模擬接口攔截請求,幫助開發(fā)者在不依賴后端的情況下高效地進(jìn)行前端開發(fā),文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-08-08
  • Vue.js?狀態(tài)管理及?SSR解析

    Vue.js?狀態(tài)管理及?SSR解析

    這篇文章主要介紹了Vue.js狀態(tài)管理及SSR解析,在vue.js中,我們主要說的狀態(tài)管理庫就是vuex,當(dāng)然,只要你能實現(xiàn)有條理的組織數(shù)據(jù),那么它都可以認(rèn)為是一種狀態(tài)管理庫
    2022-09-09
  • 基于vue開發(fā)的在線付費課程應(yīng)用過程

    基于vue開發(fā)的在線付費課程應(yīng)用過程

    這篇文章主要介紹了基于vue開發(fā)的在線付費課程應(yīng)用過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • vue中vxe-table虛擬滾動列表的使用詳解

    vue中vxe-table虛擬滾動列表的使用詳解

    vxe-table 是一個功能強(qiáng)大的 Vue 表格組件,它支持虛擬滾動列表作為其核心功能之一,本文主要介紹一下vxe-table的虛擬滾動列表功能的使用場景和優(yōu)勢,感興趣的可以了解下
    2023-12-12
  • 在vue中nextTick用法及nextTick 的原理是什么

    在vue中nextTick用法及nextTick 的原理是什么

    這篇文章主要介紹了在vue中nextTick用法及nextTick 的原理是什么,Vue.js 是一個流行的前端框架,它提供了一種響應(yīng)式的數(shù)據(jù)綁定機(jī)制,使得頁面的數(shù)據(jù)與頁面的 UI 組件之間能夠自動同步,需要的朋友可以參考下
    2023-04-04

最新評論

扶绥县| 舟曲县| 凤台县| 浪卡子县| 保亭| 竹北市| 瑞金市| 西昌市| 昔阳县| 云霄县| 阿拉尔市| 延川县| 舒城县| 醴陵市| 新竹县| 辽宁省| 许昌县| 北碚区| 英山县| 德昌县| 临沂市| 东平县| 丽水市| 尚志市| 平泉县| 木里| 水城县| 佛冈县| 霍州市| 略阳县| 遂溪县| 美姑县| 泽州县| 天水市| 仁化县| 成武县| 于田县| 炎陵县| 石门县| 鹿泉市| 博罗县|