VUE 3+TS “类型“DefineComponent<{}, {}, any>”的参数不能赋给类型“Plugin_2”的参数

文章讲述了在从Vue2迁移至Vue3过程中,遇到编辑器关于参数不能赋给类型的错误。解决方法是修改shims.vue.d.ts中的组件定义,将类型从`any`改为`ReturnType<typeofdefineComponent>`以确保类型正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue3+ts,编辑器提示报错:参数不能赋给类型。要开启什么ts配置才能去除?

vue2迁移到vue3经常发生的错误,定义好组件,main.ts报错,即使改为app.use(emp*** as any),虽然不报错,但运行时还是报错,找不到文件。

改写shims.vue.d.ts即可

const component: DefineComponent<{}, {}, any>;

改为:

const component: ReturnType<typeof defineComponent> ;

类型不写死

### 创建 Vue 3 和 TypeScript 集成 `vue-hiprint` 的演示项目 为了创建一个集成 `vue-plugin-hiprint` 到 Vue 3 和 TypeScript 的演示项目,可以按照以下方式设置: #### 安装依赖项 首先安装必要的依赖包。确保已经全局安装了 Vue CLI。 ```bash npm install -g @vue/cli ``` 接着初始化一个新的 Vue 项目并选择 TypeScript 支持选项。 ```bash vue create my-project-name cd my-project-name vue add typescript ``` 之后安装所需的插件和其他依赖库。 ```bash npm install vue-plugin-hiprint --save ``` #### 修改配置文件 编辑项目的根目录下的 `vue.config.js` 文件来引入自动导入功能和支持 Element Plus 组件解析器[^3]。 ```javascript const { defineConfig } = require(&#39;@vue/cli-service&#39;) const AutoImport = require(&#39;unplugin-auto-import/webpack&#39;) const Components = require(&#39;unplugin-vue-components/webpack&#39;) const { ElementPlusResolver } = require(&#39;unplugin-vue-components/resolvers&#39;) module.exports = defineConfig({ transpileDependencies: true, configureWebpack: { plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }) ] } }) ``` #### 设置打印组件 在 src/plugins 下新建 hiprint.ts 来定义如何使用 `vue-plugin-hiprint`. ```typescript import Vue from "vue"; import Hiprint from &#39;vue-plugin-hiprint&#39;; Vue.use(Hiprint); export default Hiprint; ``` 然后修改 main.ts 将此插件加入到应用实例中: ```typescript import Vue from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import router from &#39;./router&#39;; import store from &#39;./store&#39;; // Import the plugin here import Hiprint from &#39;@/plugins/hiprint&#39; new Vue({ el: &#39;#app&#39;, render: h => h(App), router, store, // Register it globally or locally as needed. created() { this.$Hiprint.init(); }, }); ``` #### 编写页面逻辑 最后可以在某个视图组件里调用 `$Hiprint.print()` 方法实现具体的打印操作。 ```html <template> <div class="print-container"> <!-- Your printable content goes here --> </div> <button type="button" @click="handlePrint">点击打印</button> </template> <script lang="ts"> import { Component, Vue } from &#39;vue-property-decorator&#39;; @Component({}) export default class PrintExample extends Vue { handlePrint(): void { (this as any).$Hiprint.print(&#39;.print-container&#39;); } } </script> ``` 通过上述步骤就可以完成基于 Vue 3 及其官方推荐的最佳实践——TypeScript 构建的应用程序,并成功集成了第三方打印工具 `vue-plugin-hiprint`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值