Detailed description of VueJs asynchronous dynamic loading blocks, detailed description of vuejs asynchronous

Source: Internet
Author: User

Detailed description of VueJs asynchronous dynamic loading blocks, detailed description of vuejs asynchronous

First, define the component as asynchronous loading.

define(['jquery','vue'],function($,Vue){   Vue.component('comp1',function(resolve){     require(['component/comp1'],resolve);   });   Vue.component('comp2',function(resolve){     require(['component/comp2'],resolve);   });   var b = new Vue({     el:"#app",     data:{       currentView:'comp1'     },     methods:{       toggleView:function(){         console.log(this.currentView);         this.currentView = this.currentView=='comp1'?'comp2':'comp1';       }     }   }); }) 

For details, refer to the asynchronous and dynamic components of vuejs. Then the code in html

<Div id = "app"> <keep-alive> <! -- With keep-alive, you can keep the dynamically cut components in the memory (only hidden and invisible ), however, the previous status and data will continue after the return --> <component v-bind: is = "currentView"> </component> </keep-alive> <button type = "button" v-on: click = "toggleView"> switch view </button> </div>

The advantage of this structure is that when the page is initialized and loaded, only the required component-related content will be loaded. The component that has not been switched will not be loaded, speeding up page loading. At the same time, after each component is loaded once, it is switched out and then switched back. Related content and repeated rendering are not loaded repeatedly.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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: 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.