Vue中避免滥用this去读取data中数据
目录
- 前言
- 一、用this读取data中数据的过程
- 二、Dep.target什么时候存在
- 三、在何处滥用this去读取data中数据
- 四、如何避免滥用this去读取data中数据
前言
在Vue中,data选项是个好东西,把数据往里一丢,在一个Vue组件中任何一个地方都可以通过this来读取data中数据。但是要避免滥用this去读取data中数据,至于在哪里要避免滥用,如果滥用会导致什么后果,本专栏将会一一揭晓。
一、用this读取data中数据的过程
在Vue源码中会把data中数据添加getter函数和setter函数,将其转成响应式的。getter函数代码如下所示:
function reactiveGetter() {
VAR value = getter ? getter.call(obj) : val;
if (Dep.target) {
dep.dePEnd();
if (childOb) {
childOb.dep.depend();
if (Array.isArray(value)) {
dependArray(value);
}
}
}
return value}
用this读取data中数据时,会触发getter函数,在其中通过 var value = getter ? getter.call(obj) : val;
获取到值后执行 return value,实现读取数据的目的。
这里可以得出一个结论,在Dep.target存在时,使用this去读取data中数据时会去收集依赖。如果滥用this去读取data中数据,会多次重复地收集依赖,从而产生性能问题。
二、Dep.target什么时候存在
Dep.target是由依赖赋值的。依赖又称为watcher(侦听者)或者订阅者。在Vue中有三种依赖,其中两种是很常见的,就是watch(侦听器)和computed(计算属性)。还有一种隐藏的依赖———渲染Watcher,在模板首次渲染的过程中创建的。
Dep.target是在依赖创建时被赋值,依赖是用构造函数Watcher创建。
function Watcher(vm, expOrFn, cb, options, isRenderWatcher) {
//... if (typeof expOrFn === 'function') {
this.getter = expOrFn;
}
else {
this.getter = parsePath(expOrFn);
}
this.value = this.lazy ? undefined : this.get();
}
;
Watcher.PRototype.get = function get() {
pushTarget(this);
try {
value = this.getter.call(vm, vm);
}
catch (e) {
}
return value}
;
Dep.target = null;
var targetStack = [];
function pushTarget(target) {
targetStack.push(target);
Dep.target = target;
}
在构造函数Watcher最后会执行实例方法get,在实例方法get中执行pushTarget(this)中给Dep.target赋值的。
而依赖是在Vue页面或组件初次渲染时创建,所以产生的性能问题应该是首次渲染过慢的问题。
三、在何处滥用this去读取data中数据
在Dep.target存在时去执行这些滥用this去读取data中数据的代码会产生性能问题,故还要搞清楚这些代码是写在哪里才会被执行到,换句话来说,要搞清楚在哪里滥用this去读取data中数据会产生性能问题。
在第二小节中介绍了Dep.target被赋值后会执行value = this.getter.call(vm, vm),其中this.getter是一个函数,那么若在其中有用this去读取data数据,就会去收集依赖,假如滥用的话就会产生性能问题。
this.getter是在创建依赖过程中赋值的,每种依赖的this.getter都是不相同的。下面来一一介绍。
- watch(侦听器)依赖的
this.getter是parsePath函数,其函数参数就是侦听的对象。
var bailRE = new RegExp(("[^" + (unicodeRegExp.source) + ".$_\\d]"));
function parsePath(path) {
if (bailRE.test(path)) {
return }
var segments = path.splIT('.');
return function(obj) {
for (var i = 0;
i segments.length;
i++) {
if (!obj) {
return }
obj = obj[segments[i]];
}
return obj }
}
如下所示的代码中的 a 和 a.b.c作为参数传入parsePath函数会返回一个函数赋值给this.getter,执行this.getter.call(vm, vm)会得到this.a和this.a.b.c的值。在这个过程中不会存在滥用this去读取data中数据的场景。
watch:{
a:function(newVal, oldVal){
//做点什么 }
}
vm.$watch('a.b.c', function (newVal, oldVal) {
// 做点什么}
)- computed(计算属性)依赖的
this.getter有两种,如果计算属性的值是个函数,那么this.getter就是这个函数。如果计算属性的值是个对象,那么this.getter就是这个对象的get属性值,get属性值也是个函数。在这个函数可能会存在滥用this去读取data中数据的场景,举个例子,代码如下所示。
computed:{
d:function(){
let result = 0;
for(let key in this.a){
if(this.a[key].num >
20){
result += this.a[key].num + this.b + this.c;
}
else{
result += this.a[key].num + this.e + this.f;
}
}
return result;
}
}
在计算属性d中就存在滥用this去读取data数据。其中this.a是个数组,此时Dep.target的值为计算属性d这个依赖,在循环this.a中使用this去获取中a、b、c、e、f的数据,使这些数据进行一系列复杂的逻辑运算来重复地收集计算属性d这个依赖。导致获取计算属性d的值的速度变慢,从而产生性能问题。
- 渲染Watcher的
this.getter是一个函数如下所示:
updatecomponent = function() {
vm._update(vm._render(), hydrating);
}
;
其中vm._render()会把template模板生成的渲染函数render转成虚拟DOM(VNode):vnode = render.call(vm._renderProxy, vm.$createElement);
,举一个例子来说明一下渲染函数render是什么。
例如template模板:
template>
div class="wrap">
p>
{
{
a}
}
span>
{
{
b}
}
/span>
/p>
/div>
/template>
通过vue-loader会生成渲染函数render,如下所示:
(function anonymous() {
with(this) {
return _c('div', {
attrs: {
"class": "wrap" }
}
, [_c('p', [_v(_s(a)), _c('span', [_v(_s(b))])])]) }
}
)其中with语句的作用是为一个或一组语句指定默认对象,例with(this){
a + b }
等同 this.a + this.b,那么在template模板中使用{
{
a }
}
相当使用this去读取data中的a数据。故在template模板生成的渲染函数render中也可能存在滥用this去读取data中数据的场景。举个例子,代码如下所示:
template>
div class="wrap">
div v-for=item in list>
div>
{
{
arr[item.index]['name'] }
}
/div>
div>
{
{
obj[item.id]['age'] }
}
/div>
/div>
/div>
/template>
其中用v-for循环list数组过程中,不断用this去读取data中arr、obj的数据,使这些数据进行一系列复杂的逻辑运算来重复收集这个依赖,导致初次渲染的速度变慢,从而产生性能问题。
四、如何避免滥用this去读取data中数据
综上所述在计算属性和template模板中滥用this去读取data中数据会导致多次重复地收集依赖,从而产生性能问题,那要怎么避免这种情况。
- 计算属性中如何避免
用ES6对象解构赋值来避免,计算属性的值是一个函数,其参数是Vue的实例化this对象,在上述计算属性中滥用this的例子中可以这样优化。
优化前:
computed:{
d:function(){
let result = 0;
for(let key in this.a){
if(this.a[key].num >
20){
result += this.a[key].num + this.b + this.c;
}
else{
result += this.a[key].num + this.e + this.f;
}
}
return result;
}
}
优化后:
computed: {
d({
a, b, c, e, f }
) {
let result = 0;
for (let key in a) {
if (a[key].num >
20) {
result += a[key].num + b + c;
}
else {
result += a[key].num + e + f;
}
}
return result;
}
}
以上利用解构赋值提前把data数据中的a、b、c、e、f赋值给对应的变量a、b、c、e、f,然后在计算属性中可以通过这些变量访问data数据的,且不会触发data中对应数据的依赖收集。这样只用this读取了一次data中的数据,只触发了一次依赖收集,避免了多次重复地依赖收集产生的性能问题。
- template模板中如何避免
提前处理v-for循环所用的数据,不要在v-for循环中去读取数组、对象类型的数据。在上述template模板中滥用this的例子中可以这样优化。
假设list、arr、obj皆是服务端返回来的数据,且arr和obj没有用到任何模块渲染中,可以这样优化。
优化前:
template>
div class="wrap">
div v-for=item in list>
div>
{
{
arr[item.index]['name'] }
}
/div>
div>
{
{
obj[item.id]['age'] }
}
/div>
/div>
/div>
/template>
优化后:
template>
div class="wrap">
div v-for=item in listData>
div{
{
item.name}
}
/div>
div>
{
{
item.age}
}
/div>
/div>
/div>
/template>
script>
const arr = [];
const obj = {
}
export default {
data() {
return {
list: [], }
}
, computed: {
listData: function ({
list}
) {
list.foreach(item =>
{
item.name = arr[item.index].name;
item.age = obj[item.id].age;
}
) return list;
}
}
,}
/script>
以上就是Vue中避免滥用this去读取data中数据的详细内容,更多关于Vue中避免滥用this的资料请关注其它相关文章!
您可能感兴趣的文章:- Vue2.0/3.0双向数据绑定的实现原理详解
- 关于vuex强刷数据丢失问题解析
- Vue 如何追踪数据变化
- vue+canvas实现数据实时从上到下刷新瀑布图效果(类似QT的)
- vue 数据(data)赋值问题的解决方案
- Vue 重置data的数据为初始状态操作
- Vue组件传值过程中丢失数据的分析与解决方案
- SpringBoot+Vue实现数据添加功能
- 手写Vue2.0 数据劫持的示例
- vue 数据双向绑定的实现方法
- 用vue设计一个数据采集器
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Vue中避免滥用this去读取data中数据
本文地址: https://pptw.com/jishu/594667.html
