1. ComponentHow to implement dynamic component rendering This tag is equivalent to a placeholder, and you need to use the is attribute to specify the bound component <button @click="comName = 'Left'">Show Left</button> <button @click="comName = 'Right'">Show Right</button> <div class="box"> <!-- Render the Left component and the Right component--> <!-- component is built-in Vue --> <!-- is indicates the name of the component to be rendered--> <component :is="comName"></component> </div> <script> import Left from '@/compeonents/Left.vue' import Right from '@/components/Right.vue' export default { components: Left, Right }, data() { return { //comName indicates the name of the component to be displayed comName: Left, } } } </script> 2. keep-alive2.1 Problems When a button plus one function is implemented in the The following is a function added to <div class="left-container"> <h3>Left component----{{ count }}</h3> <button @click="count += 1">+1</button> </div> <script> export default { data(){ return { count: 0 } } } </script> After adding one, switch to View the The following is the life cycle function added to export default { created() { console.log('Left component is created!') }, destroyed(){ console.log('Left component is destroyed~') } } After executing the appeal operation again, the results are as follows: Problem: When switching components, components will be destroyed and created at the same time, so each time you switch to the same component, the component object you get is not the same, and the initialization data will be overwritten 2.2 Use keep-alive to solve The Modify the App root component as follows: <keep-alive> <!-- keep-alive can cache internal components instead of destroying them --> <component :is="comName"></component> </keep-alive> 2.3Keep-alive life cycle This lifecycle can only be used when the component uses Add the following changes to the //When a component is created for the first time, created is triggered first, then activated //When the component is activated, only activated will be triggered, not created activated() { console.log('The component is activated, activated') }, deactivated(){ console.log('The component is cached, deactivated') } 2.4keep-alive include, exclude properties By default, How to specify the components that need to be cached: Use <keep-alive include="Left,MyRight"> <component :is="comName"></component> </keep-alive>
In the Left component: export default{} In the Right component: export default{ //When the name attribute is provided, the name of the component is the value of the name attribute name: 'MyRight' }
Outside the component: import Left '@/components/Left.vue' components: Left, } The registered name here is only used to reference the component. If there is no Inside the component: export default{ //When the name attribute is provided, the name of the component is the value of the name attribute name: 'MyRight' } This is the end of this article about vue dynamic component. For more related vue dynamic component content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Nginx cache configuration example
>>: HTML form and the use of form internal tags
If someone asked you whether running EXPLAIN on a...
How to save and exit after editing a file in Linu...
This article example shares the specific code of ...
This article shares the installation tutorial of ...
Table of contents Written in front Several storag...
Win2008 R2 zip format mysql installation and conf...
1. Download the installation package Download add...
Table of contents 1. Custom routing 2. Tab naviga...
1. Advantages and Disadvantages of Indexes Advant...
Table of contents Drop-down multiple-select box U...
What is a memory leak? A memory leak means that a...
This article uses examples to illustrate the prin...
This article shares the specific code of WeChat a...
This article example shares the specific code of ...
In the latest version of WIN10, Microsoft introdu...