英文:
Vue component's button callback: Cannot read properties of undefined
问题
使用Vue3+typescript,我正在尝试使用一个名为gtag-vue的库将Google Analytics事件发送到我的帐户。
在Vue组件中的想法是,当用户单击一个div
时,我将发送一个自定义事件,myCustomEvent
。
<template>
<div v-on:click="displayWhatever(index)">这里是一些内容</div>
</template>
<script setup lang="ts">
import { event } from 'vue-gtag';
let selected = ref(-1);
const myComputedVar = computed(() => {
return someVar.slice(//这里有一些返回正确数组的东西);
}
function displayWhatever(this: any, index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: this.myComputedVar[index].value1,
value2: this.myComputedVar[index].value2
})
}
</script>
每当我在开发中使用它时,通过运行npm run dev
,它运行得很完美!但当我运行npm run build
,没有错误并上传时,我在Javascript控制台中收到以下错误:
TypeError: Cannot read properties of undefined (reading 'myComputedVar')
at y (index-39a6e75d.js:1535:23997)
at onClick (index-39a6e75d.js:1535:25733)
at xn (index-39a6e75d.js:1:13194)
at cr (index-39a6e75d.js:1:13273)
at HTMLDivElement.r (index-39a6e75d.js:1:56693)
因此,看起来在生产构建中没有正确设置this
。但我真的无法理解为什么。我尝试过其他方法,比如从函数中删除this
,但然后在运行npm run build
时会遇到编译错误。
顺便说一下,我正在使用Vite,如果可能的话,请告诉我是否有影响。
英文:
Using Vue3+typescript, I'm trying to use a library called gtag-vue to send google analytics events to my account.
The idea is that in a Vue component, when the user clicks a div, I'll send a custom event, myCustomEvent
.
<template>
<div v-on:click="displayWhatever(index)">SOME CONTENT HERE</div>
</template>
<script setup lang="ts">
import { event } from 'vue-gtag'
let selected = ref(-1);
const myComputedVar = computed(() => {
return someVar.slice(//Some stuff here that returns a proper array with no errors);
}
function displayWhatever(this: any, index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: this.myComputedVar[index].value1,
value2: this.myComputedVar[index].value2
})
}
</script>
Whenever I use it in development, by running npm run dev
it works perfectly! But when I do npm run build
, with no errors and upload it, I get this error in the Javascript console:
TypeError: Cannot read properties of undefined (reading 'myComputedVar')
at y (index-39a6e75d.js:1535:23997)
at onClick (index-39a6e75d.js:1535:25733)
at xn (index-39a6e75d.js:1:13194)
at cr (index-39a6e75d.js:1:13273)
at HTMLDivElement.r (index-39a6e75d.js:1:56693)
So it looks like this
is not correctly set up in the production build. But I really can't understand why. I have tried other methods, like removing the this
from the function, but then I get compile errors when doing npm run build
.
I'm using Vite by the way, in case it makes a difference.
答案1
得分: 1
在脚本设置语法中没有this
(用于引用选项 API 中的组件实例),您应该使用computedVar.value
而不是this.computedVar
:
function displayWhatever( index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: myComputedVar.value[index].value1,
value2: myComputedVar.value[index].value2
})
}
英文:
There's no this
(that refers to component instance as in options API) in script setup syntax, you should use computedVar.value
instead of this.computedVar
:
function displayWhatever( index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: myComputedVar.value[index].value1,
value2: myComputedVar.value[index].value2
})
}
答案2
得分: 0
在一个设置(组合 API)中,您将不需要this
关键字。还要修改displayWhatever
函数的参数。它应该只接受一个参数,因此删除this
。
计算属性的值将保存在创建的引用的value
属性中。因此,您应该通过使用.value
来访问它,而不是this
。
function displayWhatever(index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: myComputedVar.value[index].value1,
value2: myComputedVar.value[index].value2
})
}
英文:
In a setup (composition api), You won't need this
keyword. Also, modify the displayWhatever
function parameter. It should accept only one parameter, Hence remove this
.
The value of computed property will be kept in a value
property of the created reference. Hence, you should access it by using .value
instead of this.
function displayWhatever(index: number): void {
selected.value = index;
event('myCustomEvent', {
value1: myComputedVar.value[index].value1,
value2: myComputedVar.value[index].value2
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论