**
浅谈vue之生命周期
**
环境搭建
在讲解vue生命周期之前我们先准备好演示环境
搭建vue的方式一般有三种
- 是使用npm搭建集成开发环境,可参考:https://blog.youkuaiyun.com/lizy928/article/details/81464244
- 引入vue.js文件,下载vue文件后和引入普通js文件一样,https://vuejs.org/js/vue.min.js
- 第三种-使用 CDN 方法
以下推荐国外比较稳定的两个 CDN,国内还没发现哪一家比较好,目前还是建议下载到本地。
BootCDN(国内) : https://cdn.bootcss.com/vue/2.2.2/vue.min.js
unpkg:https://unpkg.com/vue/dist/vue.js, 会保持和 npm 发布的最新的版本一致。
cdnjs : https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.js
本次教程采用第一种方案,在idea下打开项目后如下
启动项目并访问
这里,我们在已经搭建好的vue项目中,实现一个从项目已有的Hello World! 跳转至我们自己创建的Hello Vue组件页面的。
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/TestVue',
name: 'TestVue',
component: TestVue
}
]
})
拷贝HelloWord文件重命名为testVue.vue文件,并在在router下的index.js文件中添加路由配置。
打开http://localhost:8080/#/TestVue
接下来我们就以这个页面来讲解vue的生命周期
首先先看看官网的图,详细的给出了vue的生命周期:
可以总共分为8个阶段:
beforeCreate(创建前),
created(创建后),
beforeMount(载入前),
mounted(载入后),
beforeUpdate(更新前),
updated(更新后),
beforeDestroy(销毁前),
destroyed(销毁后)
这个是vue1.0之前的钩子函数,简单来说vue的生命周期就是体现在这些钩子函数之上。
从上图可以很明显的看出现在vue2.0都包括了哪些生命周期的函数了。
接下来写一点测试代码:
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
beforeCreate: function () {
console.group('beforeCreate 创建前状态===============》');
console.log("%c%s", "color:red" , "el : " + this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message)
},
created: function () {
console.group('created 创建完毕状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
beforeMount: function () {
console.group('beforeMount 挂载前状态===============》');
console.log("%c%s", "color:red","el : " + (this.$el));
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
mounted: function () {
console.group('mounted 挂载结束状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
beforeUpdate: function () {
console.group('beforeUpdate 更新前状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
updated: function () {
console.group('updated 更新完成状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
beforeDestroy: function () {
console.group('beforeDestroy 销毁前状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message);
},
destroyed: function () {
console.group('destroyed 销毁完成状态===============》');
console.log("%c%s", "color:red","el : " + this.$el);
console.log(this.$el);
console.log("%c%s", "color:red","data : " + this.$data);
console.log("%c%s", "color:red","message: " + this.message)
}
}
</script>
刷新页面,按F12可以看到
create 和 mounted 相关
beforecreated:el 和 data 并未初始化
created:完成了 data 数据的初始化,el没有
beforeMount:完成了 el 和 data 初始化
mounted :完成挂载
update 相关
data里的值被修改后,将会触发update的操作。
执行了destroy操作,后续就不再受vue控制了。