Implementation principle of key keep-alive in vue

Source: Internet
Author: User

Vue2.0 provides a keep-alive component for caching components to avoid loading the corresponding components multiple times and reduce performance consumption.

Keep-aliv is a built-in component of vue. js. It can store non-active component instances in the memory instead of directly destroying them. It is an abstract component and will not be rendered to the real Dom, it does not appear in the parent component chain.

It has two lifecycles:

  1. Activated: called when the keep-alive component is activated
  2. Deactivated: called when the keep-alive component is stopped

It provides the include and exclude attributes, allowing the component to cache with conditions.

Keep-alive key

?
123456789101112131415161718192021222324252627282930313233343536373839 <!DOCTYPE html><html><head>  <title></title>  <script type="text/javascript" src="./vue.js"></script></head><body>  <div id="app">    <keep-alive>      <child-component key="1" v-if="seen" name="1"></child-component>      <child-component key="2" v-if="!seen" name="2"></child-component>    </keep-alive>    <button @click="toggle">toggle</button>  </div>  <script type="text/javascript">    Vue.component(‘child-component‘, {      template: `<input type="text" placeholder="enter">`,      data() {        return {}      },      props: ["name"],      mounted() {        console.log(`${this.name} mounted`)      }    })    const vm = new Vue({      el: "#app",      data: {        seen: true      },      methods: {        toggle() {          this.seen = !this.seen;        }      }    })  </script></body></html>

Key indicates that the ID element is no longer reused. Note that key is a reserved attribute in vue and cannot be passed as a prop to the sub-component. Otherwise, the Vue error is displayed on the console.

However, the keep-alive identifier does not create component instances repeatedly, that is, only one created mounted event is triggered,

The two can be used to manage the reuse of components in a more precise manner.

 

IOS Data Storage Technology Development video tutorial 12 course with source code courseware: Baidu online storage download

Ios ui development uikit detailed usage video tutorial 43 lessons with source code handout: Baidu online disk download
Ios9 basic to master the great god class video tutorials include source code courseware interview questions employment guidance: Baidu online disk download
Full HD version of the Fourth Basic + employment course of the IOS training course: Baidu online disk download
IOS training course 6 contains source code courseware interview materials: Baidu online drive download
IOS training course Phase 5 [Full Version]: Baidu online disk download
IOS multi-touch and Gesture Recognition development video tutorial 10 lessons with source code: Baidu online disk download
Middle school swift language video tutorial video Baidu online disk download: Baidu online disk download
Geek iOS development engineer full set of video tutorial video Baidu online disk download: Baidu online disk download
Open source force iOS development from OC to Swift getting started and training video tutorial video Baidu online disk download: Baidu online disk download
Frequently Used ios ui controls use Quick Start video tutorial video Baidu online disk download: Baidu online disk download
IOS video tutorial full set of programming basics getting started app mobile app development game project practice: Baidu online disk download
IOS (iPhone) Development intermediate video tutorial: Baidu online disk download
IOS development video tutorial: Baidu online storage download
No-encryption version for iOS Phase 5: Baidu online storage download
Tom ios9 practical training for Apple App development video tutorial: Baidu online storage download
Black Horse programmer IOS video development tutorial/Xiao Jie: Baidu online disk download
IOS development technology tutorial geek college latest IOS development technology VIP tutorial getting started to proficient Tutorial: Baidu online storage download
IOS intermediate video tutorial: Baidu online storage download
IOS interview question: Baidu online storage download

 

Implementation principle of key keep-alive in vue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.