关于 Vue 3 + TypeScript 和 Augmenting-Types-for-Use-with-Plugins 的问题

2023-11-21

有谁知道如何使用 Vue3 和 TypeScript 实现类型增强的工作示例?我一直在尝试遵循 Vue2 文档,在 Vue3 中使用相同的内容,但没有成功,并且在过去 3 个小时的搜索中没有任何结果。

看来Vue对象在vue-class-component模块应该被增强才能工作,但是如何呢?

我的实现类似于以下内容:

有什么建议吗?

https://v2.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");


据我了解,vue-class-component尚未完全支持 Vue 3。他们还在讨论库中的修改。所以,我不知道下面的示例是否适用于它,但这就是我为增强插件类型所做的事情。

你好.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;
  }
}

我在插件文件本身中声明了类型,但是您可以在shims-vue.d.ts文件也。

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

你好.vue

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

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

export default Hello;
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于 Vue 3 + TypeScript 和 Augmenting-Types-for-Use-with-Plugins 的问题 的相关文章

随机推荐

  • 如何在 Vim 中每次按键时将当前缓冲区的内容写回文件?

    我希望 Vim 尽可能频繁地自动写入我的文件 理想的情况是每次击键 我需要定期保存 以便我的后台构建过程能够看到它 它是 LaTeX 文档的 makefile 我希望预览器在我完成输入后向我显示一个几乎最新的文档 最终解决方案 下面的答案有
  • genymotion player.exe 停止工作

    当我尝试启动 genymotion Android 模拟器时 它给我一个错误 player exe 已停止工作我尝试寻找答案并发现this但是 我之前遇到了 open gl 2 0 问题 为此我更新了显卡驱动程序 我的主板是华硕 P5G41
  • 更新 matplotlib 动画中的 x 轴标签

    这是一段说明我的问题的玩具代码 import numpy as np import matplotlib pyplot as plt from matplotlib animation import FuncAnimation fig ax
  • 在字符串中每 3 个字符后插入句点

    我有这个 from future import print function def f comma p string v string p string if type v string type int or type v string
  • 为什么要写 `window[ "eval" ].call( window, data );`

    jQuery 1 7rc1 第 614 行 window eval call window data 为什么不简单地写 eval call window data 答案在这里 解码 jQuery Jim Driscoll 发现对于更尊重标准
  • Git 如何处理符号链接?

    如果我有一个文件或目录是符号链接 并且我将其提交到 Git 存储库 那么它会发生什么情况 我假设它会将其保留为符号链接 直到文件被删除 然后如果您从旧版本中拉回文件 它只会创建一个普通文件 当我删除它引用的文件时它会做什么 它只是提交悬空链
  • JPanel 使用 Graphics 自定义绘图

    我有一个自定义 JPanel 有时在我的程序中 我需要调用一个将屏幕绘制为黑色的方法 就是这样 public void clearScreen Graphics g getGraphics g setColor Color black g
  • C# - 无法在 WinForms 的列表框中执行键值对

    我正在使用 WinForms 中的 ListBox 编写 C 应用程序 我从 XML 文件获取数据 ID 和全名 我想在列表框中显示全名 当我选择其中之一时 我想获取相关的 ID 我尝试使用SelectedValue财产没有运气 我也尝试过
  • MenuItemCompat.getActionView 始终返回 null

    我刚刚实施了v7 AppCompat支持库但是MenuItemCompat getActionView在我测试的每个 Android 版本中总是返回 null 4 2 2 2 3 4 The SearchView显示在操作栏中 但它不响应触
  • UDP(数据报)套接字的 FIONREAD 返回什么? [复制]

    这个问题在这里已经有答案了 哪一个做ioctl of FIONREAD返回 下一个数据包的长度 还是缓冲区中所有数据的长度 假设有一个UDP服务器接收来自客户端 1 的 2 个数据包 并在客户端 1 的数据包之后接收来自客户端 2 的另外
  • 构造函数中的异常

    在 C 中 对象的生命周期从构造函数成功完成时开始 在构造函数内部 该对象还不存在 问 从构造函数发出异常意味着什么 答 这意味着构造失败 该对象从未存在过 其生命周期从未开始 source 我的问题是 Java 也是如此吗 例如 如果我把
  • 如何从Powershell获取退出代码并返回CMD?

    我有一个 powershell 脚本 我使用 CMD 文件运行 powershell 脚本 我想从 powershell 脚本中获取退出代码并将值返回给 CMD 我试过这个 但当我执行 CMD 文件来调用 powershell 时 它不会返
  • 无法在 Visual Studio 2013 中打开 Web 项目

    昨天我安装了 Visual Studio 2013 的更新 Visual Studio 2013 Update 2 但从那时起它就无法正常工作 更新花费了相当长的时间 所以我让它运行了一夜 当我今天早上回到我的工作站时 我无法在我们的解决方
  • “Rscript”不被识别为内部或外部命令、可操作程序或批处理文件

    shell exec Rscript C R R 3 2 2 bin code R 这是对脚本的调用 调用上述脚本时 会发生错误 我试图从上述路径调用我的 R 脚本 但没有显示任何输出 在检查 PHP 的错误日志时 它说 Rscript 未
  • 为什么Matlab和Octave中inv()和pinv()的输出不相等?

    我注意到如果 A 是一个 NxN 矩阵并且它有逆矩阵 但是 inv 和 pinv 函数输出的内容是不同的 我的环境是Win7x64 SP1 Matlab R2012a Cygwin Octave 3 6 4 FreeMat 4 2 看看 O
  • 向 requestAnimationFrame 回调的函数添加额外的参数

    我希望创建一个函数 使用 requestAnimationFrame 和增量时间在 HTML5 画布上将图像元素滚动 x 像素超过 y 时间 我不知道的是 当 requestAnimationFrame allready 使用一个参数 DO
  • 上下文中的多个包:组件扫描、spring config

    如何在 spring servlet xml 文件中添加多个包context component scan元素 我努力了
  • 如何使用 TensorFlow 2.0 打乱两个 numpy 数据集?

    我想写一个函数TensorFlow 2 0比在每次训练迭代之前对数据及其目标标签进行打乱 假设我有两个 numpy 数据集 X 和 y 代表用于分类的数据和标签 我怎样才能洗牌同时 Using sklearn这很简单 from sklear
  • 扩展 Android 的语音搜索应用

    是否可以扩展语音搜索应用程序 我知道我可以在自己的应用程序中添加一个按钮来启动语音识别对话框 但我想知道是否可以扩展当您长按物理 搜索 键时自动启动的语音搜索应用程序 send text to contact message listen
  • 关于 Vue 3 + TypeScript 和 Augmenting-Types-for-Use-with-Plugins 的问题

    有谁知道如何使用 Vue3 和 TypeScript 实现类型增强的工作示例 我一直在尝试遵循 Vue2 文档 在 Vue3 中使用相同的内容 但没有成功 并且在过去 3 个小时的搜索中没有任何结果 看来Vue对象在vue class co