- What is a safe naming for custom properties for component instance?
- What is the recommended way to store component-specific but non-reactive data?
some reasoning:
While working with Vue.js, from time to time end up in a situation which I need to store some static data within the component instance. As far as I understand Vue, a component instance can be considered as a plain object with some special attributes (this.$data, .$el, .$parent, etc.). That tells me that I can do whatever I want with the object, only be aware not collide with internally used attribute names.
A common example of this is a Web Component Element that holds some other logic or even shadow DOM (e.g. Canvas for WebGl) and the reference is bound to the Vue component, meaning there's some init logic and dispose logic bound to component's lifecycle. The reference here can be a proxy object, not necessarily the DOM element itself. I usually store it in the component as a direct property with "_" prefix:
<template>
<my-custom-canvas @ready="canvasReady">
</template>
<script>
export default {
methods: {
canvasReady (canvas) { this._my_custom_canvas = canvas; }
}
}
</script>
so ad 1. is it "ok-ish" and "safe" to pollute the component instance like this? Or should I put this into the this.$data
, making it reactive data though? And ad 2. ultimately, I can't find any good guides on how to work with non-reactive data in Vue. There are cases where it feels like it should be contained in the component itself, not somewhere outside in global space. Also, is it just plain wrong or is it an edge case where there are no conventions? Could anybody give me some arguments why I should avoid custom non-reactive attributes?