Vue 3组件 ‘this’ 可能是 ‘undefined’

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

Vue 3 component 'this' is possibly 'undefined'

问题

我正在尝试在我的组件中使用 this.$refs,但无论我把它放在组件的哪里,我都会收到可能未定义的错误。我尝试过在方法、lambda 函数、生命周期钩子等中使用它,但它仍然未定义。我对 Vue 不太熟悉,我确信这是一个简单的问题。有人知道如何解决吗?

<template>
    <div ref="tabtable"></div>
</template>

<script setup lang="ts">
import { onBeforeMount, onMounted, ref } from 'vue';
import { InventoryDataService } from '@/services/InventoryDataService';
import type { IInventoryItem } from '@/assets/interfaces/iinventoryitem';
import type { IVendor } from '@/assets/interfaces/ivendor';
import { TabulatorFull as Tabulator } from 'tabulator-tables';
// TODO: Remove this!
import { TestVendor } from "@/assets/testdata/fakevendor";

let inventory = ref<IInventoryItem[]>();
let tabulator: Tabulator;
const dataService = new InventoryDataService();
const getInventory = async (vendor: IVendor): Promise<IInventoryItem[]> => {
    return await dataService.getInventory(vendor)
};

// 'this' reference below is undefined
onMounted(async () => {
    tabulator = new Tabulator(this.$refs.tabtable, {...rest of my code here});
});

onBeforeMount(async () => {
    inventory.value = await getInventory(TestVendor);
});
</script>
英文:

I am trying to use this.$refs in my component but no matter where I put it in the component, I keep getting errors that this may be undefined. I've tried it in methods, lambdas, in lifecycle hooks etc, but it's still undefined. I'm new to Vue and I'm sure it's something simple. Does anyone know how to fix this?

&lt;template&gt;
    &lt;div ref=&quot;tabtable&quot;&gt;&lt;/div&gt;
&lt;/template&gt;

&lt;script setup lang=&quot;ts&quot;&gt;
import { onBeforeMount, onMounted, ref } from &#39;vue&#39;;
import { InventoryDataService } from &#39;@/services/InventoryDataService&#39;;
import type { IInventoryItem } from &#39;@/assets/interfaces/iinventoryitem&#39;;
import type { IVendor } from &#39;@/assets/interfaces/ivendor&#39;;
import { TabulatorFull as Tabulator } from &#39;tabulator-tables&#39;;
// TODO: Remove this!
import { TestVendor } from &quot;@/assets/testdata/fakevendor&quot;;

let inventory = ref&lt;IInventoryItem[]&gt;();
let tabulator: Tabulator;
const dataService = new InventoryDataService();
const getInventory = async (vendor: IVendor): Promise&lt;IInventoryItem[]&gt; =&gt; {
    return await dataService.getInventory(vendor)
};

// &#39;this&#39; reference below is undefined
onMounted(async () =&gt; {
    tabulator = new Tabulator(this.$refs.tabtable, {&lt;...rest of my code here});
});

onBeforeMount(async () =&gt; {
    inventory.value = await getInventory(TestVendor);
});
&lt;/script&gt;

答案1

得分: 2

Template refs 在组合 API 中有所不同:

const tabtable = ref(null)

然后在 mounted 钩子中使用它:

onMounted(async () => {
  tabulator = new Tabulator(tabtable.value, {/* ...其余代码在此处*/});
});
英文:

Template refs are different in composition api:

const tabtable = ref(null)

then use it in mounted hook:

onMounted(async () =&gt; {
  tabulator = new Tabulator(tabtable.value, {&lt;...rest of my code here});
});

huangapple
  • 本文由 发表于 2023年5月28日 23:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352283.html
匿名

发表评论

匿名网友

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

确定