vue-lifecycle-vue生命周期 发表于 2017-06-23 | 分类于 vue | 本文总阅读量 次 这里的vue生命周期指的是一个组件创建到销毁的过程。 当组件激活 触发beforeCreate,Create,beforeMount,Mount当组件不再显示销毁时,触发beforeDestroy,Destroy 123456789101112131415161718<div id="app"> <h3>Let's check out the lifecycle of this hur' child.</h3> <h4>Check the console!</h4> <button @click="toggleShow"> <span v-if="isShowing">Hide child</span> <span v-else>Show child</span> </button> <hr> <div v-if="isShowing"> <app-child></app-child> </div></div><script type="text/x-template" id="childarea"> <div> <h4>Here I am!</h4> </div></script> 12345678910111213141516171819202122232425262728293031323334353637body { font-family: 'Bitter', serif;}#app { text-align: center; margin: 60px; max-width: 320px; margin: 0 auto; display: table;}.num { color: #AF007E;}button { font-family: 'Bitter'; background: #c62735; color: white; border: 0; padding: 5px 15px; margin: 0 10px; border-radius: 4px; outline: 0; cursor: pointer;}h4 { margin: 0 0 15px;}hr { border-color: #F2FAFF; opacity: 0.5; margin: 15px 0;} 1234567891011121314151617181920212223242526272829303132333435363738const Child = { template: '#childarea', beforeCreate() { console.log("beforeCreate!"); }, created() { console.log("created!"); }, beforeMount() { console.log("beforeMount!"); }, mounted() { console.log("mounted!"); }, beforeDestroy() { console.log("beforeDestroy!"); }, destroyed() { console.log("destroyed!"); }};new Vue({ el: '#app', data() { return { isShowing: false } }, methods: { toggleShow() { this.isShowing = !this.isShowing; } }, components: { appChild: Child }}); 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏