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
1. Create a test table CREATE TABLE `mysql_genara...
We may have a question: After we install MySQL lo...
This article analyzes the consistency processing ...
Use Javascript to achieve the countdown effect, f...
Now most of the Docker images are based on Debian...
This example takes the installation of Python 3.8...
In the interview, you should have experienced the...
What I want to share today is to use native JS to...
Table of contents 1. MySQL data backup 1.1, mysql...
vue-router has two modes hash mode History mode 1...
Table of contents 1. Insert 2. Update 3. Delete 1...
ElasticSearch cluster supports動態請求的方式and靜態配置文件to ...
Question: Is the origin server unable to find a r...
A few days ago, I exchanged some knowledge about ...
1. Download centos7 Download address: https://mir...