Domanda su Vue 3 + TypeScript e Augmenting-Types-for-Use-with-Plugins

Sep 29 2020

Qualcuno sa di un esempio funzionante di come dovrebbe essere implementato l'aumento del tipo con Vue3 e TypeScript? Ho provato a seguire i documenti di Vue2 a cerchi di utilizzare lo stesso in Vue3 senza successo e ho passato le ultime 3 ore a cercare senza risultati.

Sembra che l' Vueoggetto nel vue-class-componentmodulo debba essere aumentato per funzionare, ma come?

La mia implementazione è simile alla seguente:

Qualche consiglio?

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");

Risposte

A.Terra Jan 19 2021 at 03:25

Per quello che ho capito, vue-class-componentnon supporta ancora completamente Vue 3. Stanno ancora discutendo delle modifiche nella libreria. Quindi, non so se gli esempi seguenti funzioneranno con esso, ma questo è quello che ho fatto per aumentare i tipi di plugin.

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;
  }
}

Ho dichiarato il tipo nel file del plugin stesso, ma puoi dichiararli anche nel shims-vue.d.tsfile.

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");

Hello.vue

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

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

export default Hello;
</script>