How can I declare a global variable in TypeScript and use it across in Vue

Viewed 4628

I have a main.ts file, I want to declare a variable over there and use it across all the Vue files.

I have a sfc.d.ts with this content:

declare module '*.vue' {
   import Vue from 'vue'
   export default Vue
}

declare var foo: any;

I assigned a value to foo = "1234" in main.ts. How can I use this foo variable in all the Vue files? Or is there any other way, to create global variable and use it across?

1 Answers
declare global {
    const foo: string;
}

but I must say, this is an anti-pattern and bad practice (for production)

Related