Vue3 + TS 配置全局属性后ESLint出现类型不存在问题

问题:

 类型“{ $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<Readonly<ExtractPropTypes<{}>> & VNodeProps & AllowedComponentProps & ComponentCustomProps, never>; ... 10 more ...; $watch(source: string | Function, cb: Function, options?: WatchOptions<...> | undefined): WatchStopHandle; } & ... 5 more ... & Sh...”上不存在属性“$filters”。

新增加的全局属性,ESLint上识别不了,我之前的解决方案是直接在 shims-vue.d.ts上声明变量的,但是发现并没有能解决这个报错(可能是版本原因,之前确实可以这样操作)。

declare let $store: any
declare let $filters: any

后来找了一下解答。有以下解决方案:

解决:

在shims-vue.d.ts中添加或者新建一个`.ts`或".d.ts"文件。

// 代码一
//必需export 否则会导致路由找不到文件及main.ts产生错误
export {}
 
declare module 'vue' {
  interface ComponentCustomProperties {
    $filters: any
  }
}

但是我的版本里面创建项目之后就自动生成了以下代码,如果直接在declare module '*.vue'里面直接添加并没有什么效果,另外直接在我的原文件下添加 declaer module 'vue' { interface xxx }还会产生新问题: ERROR in src/views/main/system/department/department.vue:8:10(vue-router查找文件识别)

// 代码二
/* eslint-disable */
declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

最终我是创建了一个新的.d.ts文件添加代码一。

附上官方文档:TypeScript 与选项式 API | Vue.js

参考来源:vue3 + typescript 全局属性设置类型报错_何为枕边人的博客-CSDN博客

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值