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.