Vue的学习笔记:什么是ViewModel以及vue生命周期探究

1. ViewModel
2. vue的生命周期探究

#### 什么是ViewModel:
Vue的设计模式受MVVM的启发。

View:所见,即视图,Vue中应该是指模板template,用来挂载Vue实例的一个DOM元素,通常在项目根目录中index.html文件中出现,比如

Model:模型(数据),想要显示到模型上的数据,也是我们需要在程序生命周期中可能需要更新的数据。

View与Model互相分开,通过ViewModel联系起来。ViewModel就是指Vue,它负责dom监听与数据绑定。

当我们创建了一个Vue实例,

var app=new Vue({
    el:"#app",
    data(){
        retrun {
            title:"测试Vue",
            name:"weeyee"
        }
    },
    methodes:{
        
    }
})
复制代码

其中通过el与DOM联系起来,data牵着Model,methods类似controller,可以修改数据。 每当我们创建了一个Vue实例,就创建了一个新的ViewModel。

#### 如何理解vue的生命周期

vue的钩子函数:

  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • beforeDestroy
  • destroyed

总体要经过几大步骤:
1.new Vue()
2.挂载元素
3.设置数据
4.模板渲染

创建一个vue文件,简单写一个vue实例,包括el,data,以及生命周期函数。 在调用生命周期函数时打印其当时的$el,data,可观察在不同的生命时刻属性的变化。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>

<div id="app">
    <p>{{ message }}</p>
</div>

<script type="text/javascript">

	var app = new Vue({
		el: '#app',
		data: {
			message : "first message! hello !"
		},
		beforeCreate: function () {
			console.group('beforeCreate 创建前状态===============》');
			console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
			console.log("%c%s", "color:red","data   : " + this.$data); //undefined
			console.log("%c%s", "color:red","message: " + this.message)
		},
		created: function () {
			console.group('created 创建完毕状态===============》');
			console.log("%c%s", "color:red","el     : " + this.$el); //undefined
			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>
</body>
</html>
复制代码

整合结果到一张图里,如下:

参照 详解vue生命周期
Vue2.0 探索之路——生命周期和钩子函数的一些理解

转载于:https://juejin.im/post/5aacda4b6fb9a028d207b5c3

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值