Pregunta sobre Vue 3 + TypeScript y Augmenting-Types-for-Use-with-Plugins

Sep 29 2020

¿Alguien sabe de un ejemplo práctico de cómo se debería implementar el aumento de tipos con Vue3 y TypeScript? He estado tratando de seguir los documentos de Vue2 en aros de usar lo mismo en Vue3 sin éxito y pasé las últimas 3 horas de búsqueda sin ningún resultado.

Parece que el Vueobjeto en el vue-class-componentmódulo debería aumentarse para que funcione, pero ¿cómo?

Mi implementación es similar a la siguiente:

¿Algún consejo?

https://vuejs.org/v2/guide/typescript.html#Augmenting-Types-for-Use-with-Plugins

import { App, Plugin } from "vue";

export interface IHelloModule {
  sayHello: (name: string) => string;
}

export const helloPlugin: Plugin = (app: App, options) => {

  const helloModule:IHelloModule = {

    sayHello: function(name: string) {
      return `Hello ${name}`; } }; app.provide("$hello", helloModule);
};
import { Vue } from 'vue-class-component';
import { IHelloModule } from "@/hello";

declare module "vue/types/vue" {
  interface Vue {
    $hello: IHelloModule; } } declare module "vue/types/vue" { interface VueConstructor { $auth: IHelloModule;
  }
}
<template>
  <div class="home">
     .....
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';

@Options({
  components: {
  },
})
export default class Home extends Vue {
  mounted() {
    console.log(this.$hello.sayHello("World"))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
                     Neither TS nor vue-cli recognize this
  }
}
</script>
import { createApp } from "vue";
import App from "./App.vue";
import { helloPlugin } from "./hello";
import router from "./router";

createApp(App)
  .use(router, helloPlugin)
  .mount("#app");

Respuestas

A.Terra Jan 19 2021 at 03:25

Por lo que tengo entendido, vue-class-componenttodavía no es totalmente compatible con Vue 3. Todavía están discutiendo modificaciones en la biblioteca. Por lo tanto, no sé si los ejemplos a continuación funcionarán con él, pero esto es lo que hice para aumentar los tipos de complementos.

hello.plugin.ts

import { App } from "vue";

export interface IHelloModule {
  sayHello: (name: string) => string;
}

export default {
  install: (app: App) => {
    const helloModule: IHelloModule = {
      sayHello: function(name: string) {
        return `Hello ${name}`; } }; app.config.globalProperties.$hello = helloModule;
  }
}

declare module "@vue/runtime-core" {
  //Bind to `this` keyword
  interface ComponentCustomProperties {
    $hello: IHelloModule;
  }
}

Declaré el tipo en el archivo del complemento, pero también puede declararlo en el shims-vue.d.tsarchivo.

main.ts

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import Hello from "./hello.plugin";

createApp(App)
  .use(router)
  .use(Hello)
  .mount("#app");

Hola.vue

<script lang="ts">
import { defineComponent } from "vue";

const Hello = defineComponent({
  mounted() {
    console.log(this.$hello.sayHello("World"));
  }
});

export default Hello;
</script>