Vue中key keep-alive

1480 ワード

keep-alive key



    
    <script type="text/javascript" src="./vue.js"/>


    <div id="app">
        <keep-alive>
            <child-component key="1" v-if="seen" name="1"/>
            <child-component key="2" v-if="!seen" name="2"/>
        </keep-alive>
        <button>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>

</code></pre> 
 <p>key          ,  key Vue         ,    prop      ,         Vue   </p> 
 <p>  keep-alive           ,         <code>created mounted</code>  ,</p> 
 <p>                     </p> 
</div>
                            </div>
                        </div>