I am injecting the properties to the nuxt ts context like this:
~/plugins/services.ts
import Vue from 'vue';
import { errorService } from '~/services/error';
import { Plugin } from '@nuxt/types'
const services: Plugin = (context, inject) => {
inject('errorService', Vue.observable(errorService))
}
export default services
Then i have types file
~/plugins.d.ts
import Vue from 'vue';
import { ErrorService } from '~/services/error';
declare module 'vue/types/vue' {
interface Vue {
$errorService: ErrorService
}
}
declare module '@nuxt/types' {
interface NuxtAppOptions {
$errorService: ErrorService
}
interface Context {
$errorService: ErrorService
}
}
declare module 'vuex/types/index' {
// this.$myInjectedFunction inside Vuex stores
interface Store<S> {
$errorService: ErrorService
}
}
This is how i use it in the component
<template lang="pug">
div test
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
@Component({})
export default class ExampleComponent extends Vue {
mounted() {
this.$errorService.setErrorMessages('Failed to create meeting','Error message');
}
}
</script>
I also have shimed the vue files
~/shims-vue.d.ts
declare module '*.vue' {
import Vue from 'vue'
export default Vue
}
Everything is building nicely no problem, VS Code can see the type if i use this.$errorService
in mixin that is in the .ts
file, but if it is in .vue
file then shows error that this.$errorService
dosn't exist. Should i do something more to make VS Code see the type in the vue files?
@simonPuente
We endup doing this
project-vue.ts
then in the components
It is a bit annoying to add everything to
ProjectVue
class as programmers may forgot to add it there etc, but it seemed to work for us.