Does a ref unref its ref contents?

huangapple go评论51阅读模式
英文:

Does a ref unref its ref contents?

问题

I am using Vue3 with Highcharts. I need to have a reactive Object that is being passed to the chart (and when its contents change the chart is updated). It works great.

At some point I wanted to format the tooltip which appears when hovering over data. This is done in the configuration object this way:

const phishingMap = ref<Record<number, Result>>({})

const chartOptions = ref({
  tooltip: {
    formatter: function () {
      console.log(this.x)
      console.log(phishingMap[this.x])
      return phishingMap[this.x].Name + '<br>' + phishingMap[this.x].Total
    }
  }
})

I was initially returning phishingMap[this.x].Name and was having an error on phishingMap.value, to being defined. Working with Vue for some time, I got used to add .value to my reactive variables so I was quite lost.

I then added a few debug data per the code above. To my surprise, the proper way to address the Object is with a bare phishingMap and not phishingMap.value.

It is a case of the inception of a ref variable in a ref variable. I experimentally found out that the inner ref should be addressed directly and not by .value - what is the reason for that?

英文:

<sup>Sorry for the weird title</sup>

I am using Vue3 with Highcharts. I need to have a reactive Object that is being passed to the chart (and when its contents change the chart is updated). It works great.

At some point I wanted to format the tooltip which appears when hovering over data. This is done in the configuration object this way:

const phishingMap = ref&lt;Record&lt;number, Result&gt;&gt;({})

const chartOptions = ref({
  tooltip: {
    formatter: function () {
      console.log(this.x)
      console.log(phishingMap.value[this.x])
      console.log(phishingMap[this.x])
      return phishingMap[this.x].Name + &#39;&lt;br&gt;&#39; + phishingMap[this.x].Total
    }
  }
})

I was intilally returning phishingMap.value[this.x].Name and was having an error on phishingMap.value, to being defined. Working with Vue for some time, I got used to add .value to my reactive variables so I was quite lost.

I then added a few debug data per the code above. To my surprise, the proper way to address the Object is with a bare phishingMap and not phishingMap.value.

It is a case of the inception of a ref variable in a ref variable. I experimentally found out that the inner ref should be addressed directly and not by .value - what is the reason for that?

答案1

得分: 1

访问另一个引用中的函数内部的引用不会改变访问方式。您仍然需要使用 .value 来访问引用的内容。您的代码应该会给您一个"无法访问未定义属性"的错误。将其与下面的代码进行比较(始终使用 .value):

const { ref } = Vue;
const phishingMap = ref({})

const chartOptions = ref({
  tooltip: {
    x: 'foo',
    formatter: function () {
      console.log('property (x) is', this.x)
      console.log('phishingMap.value is', phishingMap.value)
      console.log('resolved property is', phishingMap.value[this.x])
      return phishingMap.value[this.x].Name + '<br>' + phishingMap.value[this.x].Total
    }
  }
})

phishingMap.value.foo = {Name: 'le Name', Total: 'le total'}
console.clear()
console.log('returned value is', chartOptions.value.tooltip.formatter())

我猜想您可能在某个地方重写了引用本身,而不是它的值,例如在加载 phishingMap 时(您确定您使用了 const 实例化它吗)?

phishingMap = await loadPhishingMap() // <--- 执行此操作后,phishingMap 将不再是引用
英文:

Accessing a ref from a function inside another ref does not change the way you access it. You still need .value to get to the ref's content. Your code should give you an "cannot access property on undefined" error. Compare it with the code in the snipped (consistently using .value):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { ref } = Vue;
const phishingMap = ref({})

const chartOptions = ref({
  tooltip: {
    x: &#39;foo&#39;,
    formatter: function () {
      console.log(&#39;property (x) is&#39;, this.x)
      console.log(&#39;phishingMap.value is&#39;, phishingMap.value)
      console.log(&#39;resolved property is&#39;, phishingMap.value[this.x])
      return phishingMap.value[this.x].Name + &#39;&lt;br&gt;&#39; + phishingMap.value[this.x].Total
    }
  }
})

phishingMap.value.foo = {Name: &#39;le Name&#39;, Total: &#39;le total&#39;}
console.clear()
console.log(&#39;returned value is&#39;, chartOptions.value.tooltip.formatter())

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; top: 0; }

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

My guess is that you are overriding the ref itself instead of its value somewhere, for example when you load phishingMap (are you sure you instantiate it with const)?

phishingMap = await loadPhishingMap() // &lt;--- phishingMap will not be a ref after this

huangapple
  • 本文由 发表于 2023年5月22日 15:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定