Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yaazel's avatar

How to use a function in any component without explicitly importing it in vue3

Hello,

How to use a function in any component without explicitly importing it in vue3?

I'm thinking, if I import in app.js it can be accessed in every component. But its not working.

This is how I'm doing it: Say I've a function in @/myjs/myfunctions.js

export function myfunc(){
//
}

in app.js

import {myfunc} from '@/myjs/myfunction'

But its not accessible in every component. What to do?

0 likes
3 replies
martinbean's avatar

@yaazel What’s the problem with importing functions? If you make it “global” then you’re just going to keep inflating your base JavaScript bundle size.

Just import what you need where you need it. Like you do in any other language or framework.

1 like
yaazel's avatar
yaazel
OP
Best Answer
Level 1

For any one wondering how to do this.

In vue there are only 3 ways of having global functions (to be used across all components)

  1. Mixins (not recommended in vue3)
  2. Plugins (make your function a plugin and use that plugin)
  3. Composables (recommended way in vue3)

Please or to participate in this conversation.