Property ‘$URL’ does not exist on type '{ $: ComponentInternalInstance; $data: {};
问题:在vue3 ts项目中定义了一个全局属性,但是使用的时候报类型错误
app.config.globalProperties.$URL = (url: string) => {
return url
}
错误
Property '$URL' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<Readonly<ExtractPropTypes<__VLS_TypePropsToRuntimeProps<Props>>> &
解决方案
- 增加一个类型文件 a.d.ts
- 文件中增加代码
export { } //这个一定要加,不加表示覆盖原来的类型定义
declare module 'vue' {
interface ComponentCustomProperties {
$URL: (key: string) => string
}
}