9

I want to add a global property to a Vue 3 application like suggested here

The prototype approach does not work with TypeScript.

I found an example that I converted into this code as config.d.ts

import Vue from 'vue'

import base from '@/config/config.json'
import dev from '@/config/config.dev.json'
import prod from '@/config/config.prod.json'

let config
if (process.env.NODE_ENV === 'production') {
  config = Object.freeze(Object.assign(base, prod))
} else {
  config = Object.freeze(Object.assign(base, dev))
}

declare module 'vue/types/vue' {
  interface Vue {
    $config: config
  }
}

I want to load some local configuration files with dev or prod scope. The scoped files will not be checked into GIT repository.

The main.ts now looks like this...

import Vue from 'vue'
import {createApp} from 'vue'
import App from './App.vue'
import Config from '@/plugins/config.d.ts'

Vue.use(Config)
createApp(App).mount('#app')

The problem:

ERROR in src/main.ts:6:5
TS2339: Property 'use' does not exist on type 'typeof import("d:/Martin/Entwicklung/2020/STA-Electron/node_modules/vue/dist/vue")'.
    4 | import Config from '@/plugins/config.d.ts'
    5 |
  > 6 | Vue.use(Config)
      |     ^^^
    7 | createApp(App).mount('#app')
    8 |

What I ideally want to achieve is a global config or $config property that can be used in the setup method of Vue 3

export default defineComponent({
  name: 'App',
  components: {},
  setup(props, context) {
    const elem = ref(context.$config.prop1)
    const doSth = () => {
      console.log('Config', context.$config)
    }
    return {doSth, elem}
  },
})

How can I fix this?

Update

After the answer from danielv so new plugin looks this

import {App} from 'vue'

export interface ConfigOptions {
  base: any
  dev: any
  prod: any
}

export default {
  install: (app: App, options: ConfigOptions) => {
    app.config.globalProperties.$config =
      process.env.NODE_ENV === 'production'
        ? Object.freeze(Object.assign({}, options.base, options.prod))
        : Object.freeze(Object.assign({}, options.base, options.dev))
  },
}

The main.ts changed also into this

import {createApp} from 'vue'
import App from '@/App.vue'
import ConfigPlugin from '@/plugins/config'
import base from '@/config/config.json'
import dev from '@/config/config.dev.json'
import prod from '@/config/config.prod.json'

createApp(App).use(ConfigPlugin, {base, dev, prod}).mount('#app')

This simple plugin can be used in the template

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <p>{{ $config.prop1 }}</p>
</template>

IntelliJ complains about unknown variable prop1, but it works.

I searched a lot but found no way to insert my $config into the setup method that is used with the composition api.

1
  • "I searched a lot but found no way to insert my $config into the setup method that is used with the composition api." - I missed this part of the conversation, this ends up being a bit of a deal breaker as I now have a global variable which I can use everywhere except in setup logic, for this reason, provide-inject approach is potentially better
    – Sam
    Nov 8, 2020 at 21:04

2 Answers 2

29

You can augment the @vue/runtime-core TypeScript module in your application:

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $config: Record<string, unknown>;
  }
}

export {}  // Important! See note.

Here is a documentation: ComponentCustomProperties

Important note: TS module augmentation works correctly only when it is placed in the module.

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

Therefore the file must contain at least one top-level import or export statement, even if empty one (as in the example above) - Type Augmentation Placement

7
  • 1
    Do not forget to add import Vue from 'vue' before augmenting the module as defined in the linked documentation. Jan 13, 2021 at 13:14
  • Soooo... this has never worked for me, Vue 2 or 3. At least Vetur doesn't like it. I'm developing a local dependency. Anyone got this to work in the same context as me?
    – Luke Pring
    Feb 23, 2021 at 10:49
  • 2
    the compiler does not like this a bit ` Module '"../../../node_modules/vue/dist/vue"' has no exported member 'defineComponent'.` ??
    – Exlord
    Apr 4, 2021 at 5:09
  • 2
    to augment the globalProperties you need on import first import { ComponentCustomProperties } from 'vue'; otherwise the compiler will complain about it
    – Exlord
    Apr 4, 2021 at 6:27
  • 1
    I have the same issue as @Exlord. Once I save my typings in src/typings/index.d.ts in a Vite based Vue project (with ts), it seems like this overrides the @vue/runtime-core typings instead of augmenting them. Every time I define a component using defineComponent, I get this TS error saying Module '"../../../node_modules/vue/dist/vue"' has no exported member 'defineComponent'. too. Any idea how to fix this? Oct 15, 2021 at 14:58
2

In Vue 3, you call the use method on the instance of your app, it's not a method on the Vue var.

const app = createApp(App)
app.mount('#app')
app.use(config)

Also, you need to write your plugin to conform to what Vue 3 expects (an object with install method).

Check out Vue 3 guide on writing and using plugins, it describe a very similar use case to yours.

By the way, *.d.ts files are intended to be used as pure declaration files to declare types, usually as a supplement to otherwise untyped modules (e.g. typing existing js code). You generally don't need to write *.d.ts files yourself when writing Typescript code (declarations can be auto generated by the tsc compiler).

3
  • Thank you for the hint. Can you please go a little more into detail? I Updated the question.
    – Nabor
    Oct 1, 2020 at 20:19
  • Coming from pure TS projects: I am wonder about your statement "You generally don't need to write *.d.ts files". How do you declare a type which is used a hundred times across your project? We have a global.d.ts file and put it there. Afaik it's a common use case.
    – aProgger
    Nov 29, 2021 at 17:50
  • 1
    Yes, it is a common practice and it's certainly valid one but generally, typescript discourages global types in favor of scoped and namespaced types. You can use d.ts files but the main purpose of those is to provide typing when they're otherwise unavailable (eg. when using untyped javascript) and you can also declare global types in *.ts files, if you must. It's not wrong, just that usually you don't necessarily need or want them.
    – danielv
    Dec 1, 2021 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.