您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

vue组件生命周期使用方法

2024/7/1 0:00:21发布28次查看
这次给大家带来vue组件生命周期使用方法,vue组件生命周期使用的注意事项有哪些,下面就是实战案例,一起来看一下。
分为4个阶段:
create/mount/update/destroy
每一个阶段都对应着有自己的处理函数
create: beforecreate created
初始化
mount: beforemount mounted
和挂载相关的处理
update: beforeupdate updated
根据要更新的数据 做逻辑判断
destroy:beforedestroy destroyed
清理工作
代码:
<!doctype html> <html>  <head>  <meta charset="utf-8">  <title>生命周期</title>  <script src="js/vue.js"></script>  </head>  <body>  <p id="container">   <p>{{msg}}</p> <!--点击的时候isshow进行取反 -->   <button @click="isshow = !isshow">切换是否显示组件</button>   <my-component v-if="isshow"></my-component>  </p>  <script>   vue.component(my-component,{    template:`      <p>       <button @click="handleclick">click me</button>       <h1>component:{{count}}</h1>       </p>    `,    data:function(){      return {       count:0      }     },    methods:{     handleclick:function(){      this.count++;     }    },    beforecreate: function () {    console.log('准备创建组件');   },   created: function () {    console.log('组件创建完毕');   },   beforemount: function () {    console.log('组件的模板准备挂载到dom');   },   mounted: function () {    console.log('挂载完毕');   },   beforeupdate: function () {    console.log('准备更新了');   },   updated:function(){    console.log('更新完成');   },   beforedestroy: function () {    console.log('准备destroy');   },   destroyed: function () {    console.log('destroy完成');   }   })   new vue({    el:#container,    data:{     msg:hello vuejs,     isshow:true    }   })  </script>  </body> </html>
生命周期练习,需要那阶段写那个阶段
<!doctype html> <html>  <head>  <meta charset="utf-8">  <title>生命周期练习</title>  <script src="js/vue.js"></script>  </head>  <body>  <p id="container">   <p>{{msg}}</p>   <my-component></my-component>  </p>  <script>   vue.component(my-component,{    data:function(){     return {      myopacity:0     }    },    template:` <h1 v-bind:style="{opacity:myopacity}">透明度将改变    </h1>`,    mounted:function(){     setinterval(function(){      this.myopacity += 0.1;      if(this.myopacity>1){       this.myopacity = 0;      }     }.bind(this),1000)    }   })   new vue({    el:#container,    data:{     msg:hello vuejs    }   })  </script>  </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue如何调用mock数据
vue路由钩子的实战使用教程
vue.js移动端组件库使用方法
以上就是vue组件生命周期使用方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product