admin管理员组文章数量:1278985
In a Vue 3 project, I have the following setup. There is a separate stuff.ts
file with some helper functions in it and I want to use it in my template.
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import { doSomething } from '@/helpers/stuff.ts'
export default defineComponent({
setup(){
onMounted(() => console.log(doSomething)) //<-- logs here okay
}
})
</script>
<template>
<!-- ERROR: doSomething is not a function -->
<a href="#do" @click="doSomething()">Do Something</a>
</template>
As far as I can tell, the function is properly imported and it's defined when I log it in onMounted()
.
But when I click the link and try to doSomething()
from the template, it says the function isn't defined. I'm new to Vue 3, but I imagine I have to do something to prep the function and make it available.
How can I make an imported function available to my template? Do I have to call a ponent method instead and use doSomething
inside that?
In a Vue 3 project, I have the following setup. There is a separate stuff.ts
file with some helper functions in it and I want to use it in my template.
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import { doSomething } from '@/helpers/stuff.ts'
export default defineComponent({
setup(){
onMounted(() => console.log(doSomething)) //<-- logs here okay
}
})
</script>
<template>
<!-- ERROR: doSomething is not a function -->
<a href="#do" @click="doSomething()">Do Something</a>
</template>
As far as I can tell, the function is properly imported and it's defined when I log it in onMounted()
.
But when I click the link and try to doSomething()
from the template, it says the function isn't defined. I'm new to Vue 3, but I imagine I have to do something to prep the function and make it available.
How can I make an imported function available to my template? Do I have to call a ponent method instead and use doSomething
inside that?
- Expose it to the tmpl via data / methods / puted. – Marc Dix Commented Jun 16, 2021 at 18:48
3 Answers
Reset to default 4The simplest way is to just "forward" it via the return of your setup function
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import { doSomething } from '@/helpers/stuff.ts'
export default defineComponent({
setup(){
onMounted(() => console.log(doSomething)) //<-- logs here okay
return { doSomething }
}
})
</script>
<template>
<!-- ERROR: doSomething is not a function -->
<a href="#do" @click="doSomething()">Do Something</a>
</template>
Return from the setup hook :
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import { doSomething } from '@/helpers/stuff.ts'
export default defineComponent({
setup(){
onMounted(() => console.log(doSomething))
return {doSomething}
}
})
</script>
<template>
<a href="#do" @click="doSomething()">Do Something</a>
</template>
For those who do not use the Composition API - Setup()
hook You can do the following:
<template>
<a href="#do" @click="doSomething()">Do Something</a>
</template>
<script>
import { doSomething } from '@/helpers/stuff.ts'
export default {
methods: {
doSomething,
}
}
</script>
本文标签: javascriptHow Do I Use an Import Function in a Vue 3 TemplateStack Overflow
版权声明:本文标题:javascript - How Do I Use an Import Function in a Vue 3 Template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266031a2368480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论