请告诉我如何编写 nuxt 插件 'printd'

Please tell me how to write nuxt plugins 'printd'

在开发过程中,我们使用Printd实现了页面打印。

创建插件/printd.ts

import Vue from "vue";
import { Printd } from "printd";

Vue.use(Printd);

nuxt.conifg.ts 的 'plugins' 部分。

plugins: [
... ,
{ src: "~/plugins/printd", ssr: false }]

但是错误如下图

10:9 No overload matches this call.
  Overload 1 of 2, '(plugin: PluginObject<unknown> | PluginFunction<unknown>, options?: unknown): VueConstructor<Vue>', gave the following error.
    Argument of type 'typeof Printd' is not assignable to parameter of type 'PluginObject<unknown> | PluginFunction<unknown>'.
      Property 'install' is missing in type 'typeof Printd' but required in type 'PluginObject<unknown>'.
  Overload 2 of 2, '(plugin: PluginObject<any> | PluginFunction<any>, ...options: any[]): VueConstructor<Vue>', gave the following error.
    Argument of type 'typeof Printd' is not assignable to parameter of type 'PluginObject<any> | PluginFunction<any>'.
      Property 'install' is missing in type 'typeof Printd' but required in type 'PluginObject<any>'.
     8 | 
     9 | 
  > 10 | Vue.use(Printd);
       |         ^
    11 | 

帮帮我!!

这对我有用:

import Vue from "vue"
import { Printd } from "printd";

Vue.prototype.$Printd = new Printd();

然后你就可以在你的整个应用中访问this.$Printd了。

尝试添加:任意,如下所示。

import Vue from "vue";
import { Printd } from "printd";

Vue.use(Printd as any);

这可能就是您的答案。但如果这不起作用,试试这个。

import Vue from "vue";
const Printd = require("printd").Printd;

Vue.use(Printd);

...如果还是不行,试试这个。 文件:~/types/index.d.ts

declare module "printd"