Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS Disable reactivity for specific properties

Tags:

vue.js

I'm using this geographic library with objects like Map and Layer and LayerTree. I'm using Vue to visualize the LayerTree.

Today I noticed however that some layers contain a lot (over 10,000) items that all become reactive, which completely explodes the memory usage. I have no need for this, because I'm only interested in a few attributes of the layer to display the layerTree.

Is it possible to declare certain attributes to be non-reactive?

like image 270
haayman Avatar asked Feb 27 '19 13:02

haayman


People also ask

Why can’t I ADD reactive properties to a Vue instance?

Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the data object in order for Vue to convert it and make it reactive. For example: Vue does not allow dynamically adding new root-level reactive properties to an already created instance.

How to initialize a Vue instance with an empty value?

Since Vue doesn’t allow dynamically adding root-level reactive properties, you have to initialize Vue instances by declaring all root-level reactive data properties upfront, even with an empty value:

Can the reactivity system track changes to an object's properties?

The only exception to this being the use of Object.freeze (), which prevents existing properties from being changed, which also means the reactivity system can’t track changes. Show activity on this post.


Video Answer


1 Answers

Anything defined outside of what's returned by data method is non-reactive. There's nothing official in a guide about this, but so far it just works.

...,
data() {
    // Nonreactive
    this.fuu = 'nonreactive'
    // Reactive
    return {
       bar: 'reactive',
    }
},
...
like image 199
xpuu Avatar answered Sep 20 '22 12:09

xpuu