简体   繁体   English

Vue.js 组件自定义非反应属性安全名称

[英]Vue.js component custom non-reactive properties safe name

  1. What is a safe naming for custom properties for component instance?组件实例的自定义属性的安全命名是什么?
  2. 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.在使用 Vue.js 时,有时会遇到需要在组件实例中存储一些静态数据的情况。 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.).就我对 Vue 的理解而言,组件实例可以被视为具有一些特殊属性(this.$data、.$el、.$parent 等)的普通对象。 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 (eg 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.一个常见的例子是一个 Web 组件元素,它包含一些其他逻辑甚至是影子 DOM(例如用于 WebGl 的 Canvas),并且引用绑定到 Vue 组件,这意味着有一些初始化逻辑和处置逻辑绑定到组件的生命周期。 The reference here can be a proxy object, not necessarily the DOM element itself.这里的引用可以是代理对象,不一定是 DOM 元素本身。 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?所以广告1.像这样污染组件实例是“ok-ish”和“安全”的吗? Or should I put this into the this.$data , making it reactive data though?或者我应该把它放到this.$data中,让它成为反应数据? And ad 2. ultimately, I can't find any good guides on how to work with non-reactive data in Vue.广告 2.最终,我找不到任何关于如何在 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?谁能给我一些论据,为什么我应该避免自定义非反应性属性?

When I need to store non-reactive data, such as consts or enums, I just put it into the data object outside of the data function, and into the created lifecycle method.当我需要存储非反应性数据时,例如 consts 或 enums,我只是将其放入数据函数之外的数据对象中,并放入创建的生命周期方法中。

When you define a variable on data outside of the data function, they are not going to be reactive.当您在数据函数之外的数据上定义变量时,它们不会是反应性的。

for example, this would initialize it as a null value, and make it available in the template if needed, but if it changes, it won't initiate a refresh.例如,这会将其初始化为空值,并在需要时使其在模板中可用,但如果它发生更改,则不会启动刷新。

<template>
  <my-custom-canvas @ready="canvasReady">
</template>
<script>
export default {
  methods: {
    canvasReady (canvas) { this.data.myCanvas = canvas; }
  },
  created() {
     this.data.myCanvas = null;
  }
}
</script>

If you're not planning on using it inside the template though, you can just put it outside the component.如果你不打算在模板中使用它,你可以把它放在组件之外。

<template>
  <my-custom-canvas @ready="canvasReady">
</template>
<script>
const myCanvas = null;
export default {
  methods: {
    canvasReady (canvas) { myCanvas = canvas; }
  },
}
</script>

I wouldn't necessarily call it a duplicate but you may find the answers to this question relevant:我不一定称它为重复,但您可能会发现这个问题的答案是相关的:

How to set a component non-reactive data in Vue 2? 如何在 Vue 2 中设置组件非反应性数据?

This topic has also been discussed by the Vue core team: Vue核心团队也讨论过这个话题:

https://github.com/vuejs/vue/issues/1988 https://github.com/vuejs/vue/issues/1988

The short answer is there's nothing wrong with adding non-reactive data directly to this .简短的回答是直接向this添加非反应性数据没有任何问题。

Vue uses _ and $ prefixes for its own internal properties (see here https://v2.vuejs.org/v2/api/#data ) so you may find that avoiding those prefixes is actually safer. Vue 为自己的内部属性使用_$前缀(请参阅此处https://v2.vuejs.org/v2/api/#data ),因此您可能会发现避免使用这些前缀实际上更安全。 From a name collision perspective it's really no different to when you're naming your props, data properties etc. as they also get exposed through properties of this .从名称冲突的角度来看,这与您命名道具、数据属性等时并没有什么不同,因为它们也会通过this的属性暴露出来。 Private properties of mixins, etc. have their own convention, outlined in https://v2.vuejs.org/v2/style-guide/#Private-property-names-essential . mixin 等的私有属性有自己的约定,在https://v2.vuejs.org/v2/style-guide/#Private-property-names-essential中进行了概述。 If nothing else I recommend reading the Detailed Explanation section as that further discusses Vue's own naming conventions.如果没有别的,我建议阅读详细说明部分,因为它进一步讨论了 Vue 自己的命名约定。

On a somewhat related note, if you freeze an object using Object.freeze then Vue won't attempt to make it reactive.在一些相关的说明中,如果你使用Object.freeze冻结一个对象,那么 Vue 将不会尝试使其反应。 This isn't really relevant to the case where the object is an HTML element but if you're just trying to keep large, static data away from the reactivity system then this can be an easier way.这与对象是 HTML 元素的情况并不真正相关,但如果您只是试图让大型静态数据远离反应系统,那么这可能是一种更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM