Vue组件的按钮回调: 无法读取未定义的属性

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

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.

&lt;template&gt;
    &lt;div v-on:click=&quot;displayWhatever(index)&quot;&gt;SOME CONTENT HERE&lt;/div&gt;
&lt;/template&gt;
&lt;script setup lang=&quot;ts&quot;&gt;
import { event } from &#39;vue-gtag&#39;

let selected = ref(-1);

const myComputedVar = computed(() =&gt; {
  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(&#39;myCustomEvent&#39;, {
    value1: this.myComputedVar[index].value1,
    value2: this.myComputedVar[index].value2
  })
}
&lt;/script&gt;

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 &#39;myComputedVar&#39;)
    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(&#39;myCustomEvent&#39;, {
    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(&#39;myCustomEvent&#39;, {
    value1: myComputedVar.value[index].value1,
    value2: myComputedVar.value[index].value2
  })
}

huangapple
  • 本文由 发表于 2023年2月27日 16:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578244.html
匿名

发表评论

匿名网友

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

确定