一、实现原理
1.vue
的底层实现原理?
底层原理的核心,vue
数据的双向绑定
2.vue
如何实现数据的双向绑定的呢? 参考文章
通过 Object.defineProperty()
方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象。
在属性的set
方法中添加监听 ->通知订阅者(订阅者被记录到集合中)
这里需要补充属性的:数据描述符和存取描述符
而get,set
是属性的存取描述符
VUE
实现数据双向绑定的效果,需要三大模块
1、Observer
:能够对数据对象的所有属性进行监听,如有变动可拿到最新值并通知订阅者
2、Compile
:对每个元素节点的指令进行扫描和解析,根据指令模板替换数据,以及绑定相应的更新函数
3、Watcher
:作为链接Observer
和Compile
的桥梁,能够订阅并受到每个属性变动的通知,并执行指令绑定的相应回掉函数,从而更新视图

3.vue
的生命周期?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>vue生命周期学习</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
</head>
<body>
<div id="app">
<h1>{{message}}</h1>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
message: 'Vue的生命周期'
},
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>
</html>
4.什么是MVVM
开发模式?
MVVM
分为Model、View、ViewModel
三者。
Model
代表数据模型,数据和业务逻辑都在Model
层中定义;
View
代表UI
视图,负责数据的展示;
ViewModel
负责监听 Model
中数据的改变并且控制视图的更新,处理用户交互操作;
Model
和 View
并无直接关联,而是通过 ViewModel
来进行联系的,Model
和 ViewModel
之间有着双向数据绑定的联系。因此当 Model
中的数据改变时会触发 View
层的刷新,View
中由于用户交互操作而改变的数据也会在 Model
中同步。
这种模式实现了 Model
和 View
的数据自动同步,因此开发者只需要专注对数据的维护操作即可,而不需要自己操作 dom
。
5.delete
和Vue.delete
删除数组的区别
delete
只是被删除的元素变成了 empty/undefined
其他的元素的键值还是不变。
Vue.delete
直接删除了数组 改变了数组的键值。
var a=[1,2,3,4]
var b=[1,2,3,4]
delete a[1]
console.log(a)
this.$delete(b,1)
console.log(b)

5.Vue
中的通信方式
这篇文章不错:juejin.cn/post/684490…
6.vue
路由传参数 官方地址
使用query
方法传入的参数使用this.$router.query
接受
使用params
方式传入的参数使用this.$router.params
接受
注:此文章会根据我本人的学习情况不断更新