英文:
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<Record<number, Result>>({})
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 + '<br>' + 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: '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())
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- language: lang-html -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<!-- 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() // <--- phishingMap will not be a ref after this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论